Android : Some Questions to Understand GCM well - android

I am Trying to fetch and push A notification (To inform the device to connect to the data source because it has been changed/Updated)
I read A lot of tutorials about it , and tried to apply it to help me in my case , but really i faced some problem understanding the GCM HOW IT Works
I've register at https://code.google.com/apis/console and take the SENDER ID AND APIKEY
Now I've Some Questions :
do you register the device to the service every time we start the application or only for the first time ?
do we use GCM for (only notify the device something happens) or to (notify and get the information about whats is changes) ?
why this operation need (Client And Server)
I mean (Client) it's OK Because he'll receive the push notification ,, but (Server What it Means Here) :( ,, because i think the server here will be the Google it self to notify the device
In this Case When We Use A APIKEY
the Idea Is :
I've SQL Server Database on my web-site and have small android application (Dealing With it) with two users
need when any user Add A record to the database notify the other user
Thanks In Advance ,,
Regards :)

1) You register with GCM only when you need to. E.g. When you call
GCMRegistrar.getRegistrationId(appContext);
And it returns a blank string. Typically this happens the first time you launch your app.
2) It depends on what the data you are trying to is and whether or not it will fit within the 4K payload limit. Take a look at the Send-to-Sync vs. Messages with Payload Google Help topic.
3) Your server is the server that sends the message to the GCM servers. The GCM servers then send the message down to the apps. Take a look at this blog post I wrote for a service that I helped create: http://blog.andromo.com/2012/how-does-airbop-push-messaging-work/ It should help explain how this works. In the explanation you can just substitute our servers with your own.
4) You send your API key as part of your message to the GCM servers as detailed here: http://developer.android.com/google/gcm/gcm.html#request
So in the following request:
Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA
{
"registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
"data" : {
...
},
}
Authorization: key=YOUR_API_KEY

Related

Is it possible for Android to retrieve FCM notification if the notification was sent when it was offline

I'm using firebase cloud messaging, and I'd like to know if it's possible to send a notification to an offline device and receive it after it comes online afterward.
Client side
I send notifications via topics to a virtual device (Nexus 5 API 27)
I use the following dependencies :
compile 'com.google.firebase:firebase-auth:11.8.0'
compile 'com.google.firebase:firebase-core:11.8.0'
compile 'com.google.firebase:firebase-database:11.8.0'
compile 'com.google.firebase:firebase-messaging:11.8.0'
Server side
I have a Java server that sends POST requests :
URL :
https://fcm.googleapis.com/v1/projects/PROJECT_ID/messages:send
Headers
Authorization : Bearer <token OAuth2>
Content-Type : application/json
Body
{
"message": {
"topic":"topicName",
"data": {
"infoKey": "infoValue",
}
}
}
When the virtual machine is started I receive the notification quickly.
But if my virtual device is shut down and my server sends a notification, the client doesn't receive the notification even after waiting more than 10mn.
Is it normal ?
I feel like I did something wrong because when I read the documentation the notification is supposed to be stored until someone reads it :
https://firebase.google.com/docs/cloud-messaging/concept-options#ttl
Does anyone knows if I made a mistake or perhaps do I count too much on the feature of FCM ?
thank you.
That should be the correct behavior. When the device is offline or in doze it will deliver it as soon as it feels like it needs to or when the device wakes up. https://firebase.google.com/docs/cloud-messaging/concept-options#lifetime
You could try playing around with priorities to see if that delivers notifications faster once the device is turned on, if you need it to be as soon as possible.
You are going good.
But As off now Google do not provide any history feature on user basis.
I can suggest a simple approach to this problem. You can keep a record of the notifications you send to users at server end. At users end, whenever notification is received maintain another record.
And whenever user goes online just tally both the records and fetch missing records using restful service. You can use various parameters for tallying the records like timestamp, tokens, etc.
Hope this helps.

FCM push notifications with Parse Server

This is complicated but I'll try to be as concise as possible. I am using FCM to send push notifications to Android devices. My backend is parse server. I can successfully send pushes from the FCM console to the device. And when I send pushes from parse to https://mybackend/parse/push, it says {"result": "true" } as the response. I have checked the server logs, and it (predictably) says all http post requests to /parse/push were 200 success.
However when sending from parse (using curl or the parse push console), the pushes do NOT show up in FCM console. There is some issue between parse server and FCM then.
In my index.js, I have:
push: {
android: {
senderId: "XXXXX",
apiKey: "YYYYYY"
}
}
I have the keys, and done everything according to here: http://parseplatform.org/docs/parse-server/guide/#push-notifications
what else am I missing? Why can't I get this to actually appear on the device? Any help is greatly appreciated, thanks.
Setting up a Parse Server for an Android app, I remember having to double check this to get it right. The key for me was this: when I logged into Firebase console and selected the app I was working on, and clicked on the gear [or cog or whatever] and then Project Settings, I had to make sure to click on the Cloud Messaging tab and grab the Legacy server key [under project credentials] NOT the web api key that appears on the General tab. Also the sender ID that you want to use appears on that page.
I'm not sure when they will deprecate the Legacy server key, you might try the Server key instead, but definitely make sure you get the info off of the Cloud Messaging tab.
I don't know why FCM (and APNs) don't send a different message when the key (or cert) is not set up right [that would be more developer-friendly at least] and instead just send a 200 but my guess is that they evaluate the key [or cert] after receiving the intended notification and sending a response.

How to check the delivery status of Firebase message sent to an Android application? [duplicate]

This question already has answers here:
Firebase notification records/log API
(2 answers)
Closed 4 years ago.
I am testing the Firebase JSON to check the delivery receipts of the notification sent to the news app. I can successfully send the message to the Android app without any issues and I receive the message on my Android app. However, I want to know how and where can I check if the notification was successfully delivered to the Android app?
How do I use the message_id and/or multicast_id that is given back to get the delivery status of the notification? I can't find working code examples of checking for delivery status with Firebase. Does anyone have working JSON code to query the delivery status of a Firebase message via the message_id and/or multicast_id or another way? The Firebase documentation does not give examples on how to do this (or maybe I missed the example).
Please see the Firebase JSON below used to send the notification to the Firebase news app. Anyone's help is highly appreciated.
In the header I set authorization key=firebase_server_key
and content type is set to application/json
{
"to" : "token received from firebase",
"notification" : {
"body" : "test",
"title" : "Alert",
"icon" : "myicon",
"delivery_receipt_requested": true
}
}
Output from postman:
{"multicast_id":7845213569874521,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:4853214789631%31bd1c9631bd1c96"}]}
Update:
The Diagnostics Tool has been removed since Nov. 30, 2017:
You can no longer access FCM diagnostics data in the Play Console.
Confirming what #user7410521 said in the comments section. There is currently no available API to make use of the message_ids/multicast_ids to retrieve the details of the delivery status of the message sent, other than using the FCM Diagnostics Page. Do also keep in mind that the Diagnostics page is only usable when the app is already published (for Alpha testing or later).
I noticed that you included the delivery_receipt_requested parameter in your payload. Receiving the a message confirmation should be enough to determine that the device received the message.
Though there is no API so far also I could find out. I have tried a workarond. From the server side PHP I am sending an FCM message. Let me present how I have handled this in PHP.
$fcm_return = fcm_message_send($message);
$fcm_return_JSON = json_decode($fcm_return, true);
echo $$fcm_return_JSON['success'];
As I am sending a message to one device, I am comparing return with 1. Based on this you could further implement your logic.

Android PushNotification From Json URL

I have a Json URL, which contains data about Latest Job Postings, I am successfully parsing the Json URL and able to display the top job postings in my ListView.
But my requirement is to create a push notification, so that whenever a new job is posted, the user should be able to get a notification on device.
I have followed this: http://www.vogella.com/articles/AndroidNotifications/article.html
But I don't know how to get notifications in my case.
Could anyone help me?
Issue:
Give push notification to user's device about the updated data even when application is in background mode.
Solution:
Upon successful insertion of new data in your database (which is going to give updated set of data to your JSON request) , just call the file which send GCM push notification to all your users.
Reference:
GCM docs
GCM push-notification using php server
In context of implementation presented in demo app of 2nd link,
upon successful insertion,you can call send_message.php file,but make sure that $regId and $message should be retrieved from your database
You have created ActionBar Notifications for your app, but now you need to create the ability to receive notifications from a web client, instead of going to find them yourself from the URL.
To create a push notification you would need to have a constant thread (BroadcastReceiver) on the device that is waiting for the notification from the sever.
Google 'Cloud to Device Messaging' is the simplest way to do this.
This is a good link with lots of info on how to do this :
http://blog.mediarain.com/2011/03/simple-google-android-c2dm-tutorial-push-notifications-for-android/
If you require these notifications to be displayed on the device even when the application is not running (which seems to be the case from what you describe), you can use Google Cloud Messaging.
You would need a server that would poll the Json URL for updates, and send a GCM message to all the devices where your app is installed once such an update is detected.
Your app would have to register to Google Cloud Messaging and send the Registration ID received from Google to your server.
When your app receive a GCM message, you would create a notification and when the notification is tapped, you would start the activity that loads the data from the JSON URL.

How to push XML file from server to android application via GCM server

I am trying GCM based android app to push messages from server to android client. I am able to push fix string with the following coe. I am wondering about the ways to push XML file from server and parse at the android application. I have done some research but I couldn't find push XML rather I found send XML file. Thank you
if (androidArray.size() == 1) {
String registrationId = androidArray.get(0);
Message message = new Message.Builder()
.collapseKey(collapseKey)
.timeToLive(30)
.delayWhileIdle(true)
.addData("message", Message)
.build();
Result result = sender.send(message, registrationId, 5);
You don't push xml (or JSON preferably) to the android app. You send a simple message to the app.
when the app receives the message it then needs to go and pull the xml/json from the website with an http get request to the relevant url that will supply the xml.
The android app can then parse the response and do whatever you want it to.
Here is an EXCELLENT tutorial on C2DM (The forerunner to GCM) http://www.vogella.com/articles/AndroidCloudToDeviceMessaging/article.html
You should be able to work out the differences needed.
UPDATE
Google Android has a complete section on GCM which can be found here
http://developer.android.com/google/gcm/index.html
Within that link there are getting started guides and a GCM Demo app
There are limits to the amount of data you can send and you should not rely on your data not ever exceeding the limits or Google arbitrarily changing the amount of data you are allowed to send.
Should either of those occur you would need to update your app so just do it right in the first place.
The message you send should act as a "key" to determine what action to take when the message is received.
UPDATE
If you are feeling REALLY adventurous you could use a custom sync adapter to help you consume your web services. It's pretty advanced stuff but if you are feeling curious about this then watch the Google I/O seminar on consuming RESTfull web services http://www.youtube.com/watch?v=xHXn3Kg2IQE

Categories

Resources