Getting a unique RegistrationID of a Android device to C2DM - android

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");

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

GCM: Getting an already registered ID from GCM Server

I'm trying to implement push notifications using Google's GCM for Android and I had a question about it. In the demo google provided, they have the program registering with the Play Services and getting a registration ID which is then sent to the GCM server. How can I obtain that registration ID that is sent to the server so that I can use it to send a message from my 3rd party server?
So far, to get the registration ID that my program generated, I just display it on the application itself but if one didn't have access to the device that's generating the registration ID, how would that work?
You should store the ID on your server in some way. Whether this is in a plain text file or a database, I wrote my back-end in Python with Flask. You can associate with it a username and password,if you have user's register for an account. That way, you can store their device ID and send the respective messages to their device.

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?

Do I need different Authentication Token for each user for sending message using C2DM?

I know there is unique registartionId get from users email address for sending message to his device, but also do i need to have different authenctication id for each user?
If I am right we just need registrationId to send message to user, where as AuthenticationId is static, which is use to send message to user with registration id.
but in my case, I got Authentication Token using my developer email address, and if send message to the same email address I receive message. and If I send message to another user using registration id received from his email address and using authentication token got from my developer account email address user didn't receive message.
and again if I use authentication token got from user's email address and password and using his registration id user receive message.
The authenticationToken confirms that you have a valid Google account and thus are in general allowed to send C2DM messages. The registrationToken is directly tied to a user's device and your application. It allows your application to use your authenticationToken to send a message to the user's device.
You need one authenticationToken to send messages to all devices for which you have registrationTokens. Note however, that you should pick up a new authenticationToken every time you start sending messages, as those can be changed by Google at random.
I recall reading that Google advises to use different mail accounts for sending and receiving.
To the best of my knowledge the Authentication token is generated against the c2dm gmail id and the gmail account configured on the device.
That may be the reason why the other user is not receiving messages using your registration id.
So the answer to your question is yes, you need to have different Authentication Token for different user for sending message using C2DM.
You're correct that the authentication token is static, it serves to authenticate the app to the C2DM service. The registration id serves to identify the user you are sending the message to.
Since you are using your developer email account, it might be that the authentication token is invalidated. Did you check that? You'd get a 401 error sending the message. You shouldn't be using or collecting authentication tokens from your users, that shouldn't even work.
Note that C2DM assumes you are using a server component for your app as described here: http://code.google.com/intl/nl/android/c2dm/index.html#arch.
If you are trying to send a message to the C2DM server from the device, you're doing it wrong.

C2DM registration ID clarification

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).

Categories

Resources