C2DM registration ID clarification - android

I am new to C2DM. I have few question about C2DM registration ID.
I have installed same C2DM Mobile app in two devices. Whether I will need two rigistrations ID for two devices or we need one registration ID for mobile client.
My requirement is I have to install client app in two devices and I need to notify both the devices same time when I changed data in Server.
Suggest me the RegistrtionID implementation.

each device can register separately and will receive a separate registration id. When it's received, you'll send the registration id to your application server.
However the registration id is stored on your server, you'll want to send separate notifications for each device that you want to receive a notification (as identified by its registration id).

Related

How Android GCM works?

I was wondering how android push framework is able to distinguish data recieved via GCM and forward it to the appropriate android application for which it was intended ?
Can anyone let me know how it is done ?
You question : how android push framework is able to distinguish data recieved via GCM and forward it to the appropriate android application.
From your question it looks like you willing to know about data
workflow & Client Server architecture that exists in applications
which are using the GCM service.
As per google’s documentation “Google Cloud Messaging for Android (GCM) is a service that helps developers send data from servers to their Android applications on Android devices”. GCM is a service provided by Google for developer that helps developer to send data from server to any number of Android devices.
Simplified Application Specific Work-flow:
The push notification can be broadcasted either to the mass audience
or a select set of users. Mass audience is targeted when the
notification has to be sent about a marketing campaign. A subset of
users are targeted when a personalized information has to be sent.
The below steps explains how push notification works on android devices:
First android device sends sender id, application id to GCM server
for registration.
Upon successful registration, GCM server issues registration id to android device.
After receiving registration id, device will send registration id to our server.
Our server will store registration id in the database for further use.
Whenever push notification is needed, our server sends a message to GCM server along with device registration id (which is
stored earlier in the database).
GCM server will deliver that message to respected mobile device using device registration id.
This can also be understand using following figure
An Example Workflow:
So, from above images it easy to understand that whenever the android
application is first installed by the user, then it registers itself
to GCM server, and obtains unique GCM ID, then it's our Host servers
responsibility to keep this newly registered Registration ID of the
android user into Database, and then it will be used whenever server
side application willing to send the message to that particular
android user.
So, let us consider one case; suppose an Server wants to send Some data to Android User, which has already registered it's GCM ID 1234567 when it's first time installed, and as it's in the server's database the server application will fetch it from DB, and simply make a HTTP POST request to the GCM server in JSON format, which will have registered user's GCM ID along with the data to send , in same way the GCM Server has the record of all the Registered GCM/Android Clients, it directly forwards that message to the intended android user, and android app in user's phone will raise and Notification alert, to indicate an push notification has arrived.
Hope This answers an question!
GCM stands for Google Cloud Messaging.
Every push notification receive on any Android device is sent by the GCM only.
sender -> GCM -> Android Device
when sender sends an push notification then it goes to GCM. GCM receives that push and forward it to particular Android Device by its Unique device id.
GCM can't deliver Push without Unique Device ID.
while implementing push notification there are two important things, application key and server key ... these are unique Keys.. using these keys GCM identifies the application to whom push notification is related

Should I generate android GCM registration ID app specific or user specific?

I am developing an app which has login functionality.I am generating only one gcm registration ID per app. If other user sign's in in same app he will start receiving notifications intended for the previous user.
How to handle this situation, so that each user will get notification intended for his/her?
Should I generate gcm reg id for each user?
What is the standard way to handle this situation?
You could try the following things:
When the user logs off, send a request to delete the token on your server, and erase it on your app;
Once the user logs off, you could simply remove the association of "User ID" to "GCM Token" (Or Registration ID) on your server. And when someone logs in again, you make a new association to that Token.
The GCM Token is app specific, but the association you make on your server is totally up to you.
And I can't stress it enough, the token generated by GCM is APP SPECIFIC. If your user logs in on multiple devices, your server should handle that, associating the user ID to multiple Registration ID Tokens.
An ID issued by the GCM servers to the Android application that allows
it to receive messages. Once the Android application has the
registration ID, it sends it to the 3rd-party application server,
which uses it to identify each device that has registered to receive
messages for a given Android application. In other words, a
registration ID is tied to a particular Android application running on
a particular device.
And from the docs, your server should also:
Before you can write client apps that use GCM, you must have an
application server that meets the following criteria:
Able to communicate with your client.
Able to send properly formatted requests to the GCM connection server.
Able to handle requests and resend them using exponential back-off.
Able to store the API key and client registration tokens.
Able to generate message IDs to uniquely identify each message it sends. Message IDs should be unique per sender ID.
EDIT: You can read more about GCM here.
For further reading, you could also read more on Device Group Messaging.
You can also download some code samples here.
You should store regID as per user.
This is because there is test cases that user1 logs out and user2 logs in. In that case if you have stored regID app specific and not binding with user then user2 will also get notification.
So you need to store regID as per user , app and as well as device specific.
Of course yes every time someone downloads your app, your app should register them with the GCM and be given a reg token, this needs to be sent to your app server, on your app server you need to sort out who you are sending the notification too. you should probably send some login info and reg token when they login so you can identify each person, then write some php or Jquery to send to individual users or all. you should also send the reg token every time they login as these can change without warning.
Your app server should be able to handle removing unused reg tokens and adding new ones
Each android device generate a Unique GCM reg id. push notification from server side will send according to the device id. So at Login time you can send GCM reg id to your server. So from server side push notification will send to device.., that is actual functionality.
Hope this will help

Google Cloud Messaging send notification to all devices

I'm new in GCM. I would like to send an message to all devices that have the app installed. I read about registration_id: after the first connection to GCM, google send this unique string to device. I'm a beginner in server world but if I'm not mistaken, in server side, for sending a notification to devices I have to send array of registration_id and the message to google.
Google knows how has the registration id?
Is there a way to send messages to all devices without pass the registrarions id?
Thank you.
With GCM 3.0 it's now possible to send a notification to all devices thanks to topics support. The app must suscribe to one or more topics and the server can send notifications to that topic without specifying individual devices.
https://developers.google.com/cloud-messaging/topic-messaging
You can suscribe all devices to a topic called "global" and then send the message to "/topics/global" instead of sending them to all the registration_ids.
Is there a way to send messages to all devices without pass the registrarions id?
No way.
After successfully registering on GCM, you (the Android application) should send the registration id to your application server and store them somewhere, in a database for example. This registration id will be used to send a notification to a particular device.
To send a notification to all devices, would mean then to select all the registration ids from that database, and as you said, put them in an array and pass them further to GCM.
Update: With Firebase Cloud Messaging, it is now possible to use https://firebase.google.com/docs/cloud-messaging/android/topic-messaging to send notifications without explicitly specifying registration IDs.
You need to send the list of reg id of devices and also this list should not exceed 1000 this is a limitation of GCM if you want to send message to more than 1000 devices then you need to break the list in chunks of 1000.
YES, there is a way to send a message to all!
Just send in the 'to' field the '/topics/global' value, rather then in the 'registration_ids' field the ids.
For example in php:
'to' => "/topics/global",
and not this:
'registration_ids' => $this->devices
Create the notification_key, which identifies the device group by mapping a particular group to all of the group’s associated registration tokens(You can create notification keys on the app server).
With a notification_key , instead of sending one message to one registration token at a time, the app server can send one message to thenotification_key , and GCM then sends the message to all of the group’s registration tokens.
Also note that maximum number of members allowed for a notification_key is 20.
Google Dev site has added a new guide for this topic in particular.
https://developers.google.com/cloud-messaging/notifications#sending_downstream_messages_to_device_group
I think there is a confusion here. I had used the github sample code (app server in Java deployed to Tomcat for example) and Android app. There, I didn't "pass" or "send" any registration Id to the app server. It called the relevant APIs to retrieve the registration IDs and use them to send notifications. Why is every thread about GCM registration ID saying that one needs to pass registration IDs to 3rd party app server? I am afraid I don't agree. I think 3rd Party app server can query GCM server itself to find out which devices have registered to receive notification from a particular sender (sender id). Having to manually pass the registration IDs to 3rd party app server defeats the whole purpose of automating the process. Maybe I am missing something here or I am using the deprecated content. Anyway, how can an automated process involve manual intervention once it starts?

Send push notification for one app

I have many application on android. And some users are share the same register ID. How can I specify and send the push notification to a specific app?
Different applications have different registration ids on the same device, so you shouldn't have any problem. What do you mean by users are share the same register ID? Do you mean users of the same application on the same device? That's the only case where they can share a registration ID.
Even if you use the same Google API project ID when you register the different Apps for Google Cloud Messaging, you'll still get a different registration ID for each app. I tested it.
If you cannot change the registration ID process to uniquely identify you applications (the best solution), you can send a payload with your GCM message and have your application ignore GCM messages without a specific payload.

Getting a unique RegistrationID of a Android device to C2DM

To use C2DM for my application i registered new Gmail account with Google as a Sender account.
Now i am confused that how to get RegistrationID for my device and how to associate it with the Sender account. i want to install this application on multiple Andorid phones so do i need unique RegistrationID for each Android phone .
There are two sides to C2DM:
Your server stores registration IDs for the users. It requests n auth code from Google's servers (using ClientLogin) and then uses that auth code to send push messages.
Your Android app requests a registration ID from Google's servers. It is sent one over C2DM. It sends it to the server, to be stored there (so messages can be sent to it).
This tutorial describes it all in detail, with code:
http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html
A registration ID is like the address on a postal envelope - it describes where a push message is to be sent, the destination. So yes, you need one for each device. Registration IDs are actually assigned to a specific app (name space) on a specific device.
If you have added the boiler plate C2DM code from http://code.google.com/android/c2dm/ ,when the app registers, it will recieve a registration id , which you have to save it in a database on your server. The registration id can change so you have to notify the server, every time it changes.
To get the registraton id use
C2DMessaging.register(this, "email id");

Categories

Resources