How does server locate GCM (Google Cloud Messaging) registration IDs - android

I am trying to write an application which notifies a cellphone using GCM. I was able to implement basic functionality, but there is one question I'm still unsure of
As I understand, the flow goes like this:
Device start up
Device registers, obtains registration id
Server gets the knowledge of registration id on device
Server is using it's API key + registration id to send payload to device
Device receives payload.
One thing which is unclear to me is: what is a proper methodology to implement step #3?
I could not find any way to obtain a list of registered devices. Lets imagine, I am using GCM for a purpose of posting to a known device, and I could email that ID to myself and then register it on the server manually. But as I understand, it expires.
So, it there an expectation that I have to keep re-registering and somehow notifying server every time it happens?

Yes, your app on device must send registration ID to your server application in order for server to be able to send GCM messages. See third point in Enabling GCM.

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

Android Authentication and GCM Push notifications

I'm looking to create for my app some authentication. This will basically consist of a user registering (preferably with their google account, although not necessary) and providing a username that is then sent to my server and then having the ability to log in. I then from there would like to be able for the client app to communicate with my server by sending data and receiving push notifications via GCM (google cloud messaging). It is important that the server is able to send a push notification directly to a certain user not to all registered devices.
Would someone be able to point me in the right direction for what I must do? I've had a look at a few tutorials but what I've seen has always sent messages to all registered devices not to a specific user.
Some extra information about the app:
-The client app (user) will need to send a piece of information to the server. This information will also include the user that it needs to be sent to. Once the server receives the piece of information it should send a push notification through GCM to the specified user's client app.
I'm clueless as to how I need to go about setting up the authentication system, any help would be very appreciated!
Thank you very much,
Daniel
Offcourse you can send push notifications to selected users. This process would roughly comprise the following steps:
1) send the User's GCM registration id to your own server along with a unique user id (what ever id you have assigned to that user; could be email or sim id or something like that). Save it in your database.
2) To send Push notifications to selected users, choose users from the database based on some criteria, retrieve their GCM registration ids from the database and forward them the GCM server.
but there is one important thing you must handle. GCM registration ids could change e.g if user uninstalls and re-installs the application. You must update you corresponding database entry in that case.

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?

GCM: Send notifications only for the current user logged in application

For the application that I am working I need to integrate Google Cloud Messaging. After playing a little with different examples, I was able to send and receive a notification on my deivce.
However, I have come across an interesting situation. As I know (please correct me if I'm wrong), the registration_id is issued per device and per application.
The application that I'm working supports login functionality. When the application is installed and the user logs in for the first time (let it be "UserA"), I request the registration_id from GCM which then I send it to my server.
Now imagine that UserA logs out and gives his device to some UserB to log in. With other words, UserB logs in using UserA's device.
The problem is that if meanwhile UserA receives a notification, UserB will be able to intercept it. And if UserB receives a notification, he won't be able to receive it.
This seems normal because the registration_id is per device and per application, but it does not seem reasonable for my case.
So I'm asking if there is a way I could make the registration_id to be dependent of some user id (besides the device and app)? Or how could I make such that the logged in user to receive only his own notifications?
Yes its true that you have one google registration ID for the app per device.
But you can always register and unregister users at your own Server that will actually send messages to GCM and GCM will send this to the registered devices.
Define some interfaces for your Server like registerOnServer and unRegisteronServer , send some unique value for each user on this interface.
So, in your case, when A use Log ins , regitration is done on GCM first and register the user on your Server with registerOnServer inteface and while the user is logged in send notifications pertaining to him to GCM to be send to the device.
When A logs out, unregister him using unRegisterServer and do not send any messages from your Server to GCM as A is unregistered.
So,now if B Logs in even with the same device , register him at your Server and send his messages.
This shall solve your problem!

Best practices implementing C2DM registration

I'm developing an application that uses C2DM to receive push notifications. I've implemented the whole C2DM circuit (both client and server) and it's working fine.
Currently my applicacion has a button to bootstrap the C2DM registration, when receiving the registration id token from Google I call a webservice in my app server to associate the device with the registration id.
I'm going to implement authentication in my application and I have a few question related to the handling of the C2DM registration.
The client application (ie the Android one) will have a login screen as the first screen so the user can enter the credentials. As soon as the credentials get validated I'm planning on calling the C2DM registration so the user gets associated with a registration id token. Is this ok? In later executions of the application I will probably store the credentials or some sort of token so the user doesn't need to enter the credentials again, Should I also fire the C2DM registration when the application launches?
I'm aware that Google may eventually update the registration id. Is it a good practice to also update the registration id on a regular basis? If so, when should be appropiate? Does the registration id token expire?
What happens in the rare case of a desynchronization of the registration id between the client and the server (eg a new registration id arrives at the client, in the middle of that a new event is fired on the server with the old registration id, then the registration id arrives at the server)? Will Google handle this cases? Should my app server handle this cases?
What happens if the server is not reachable when a new registration id arrives from Google? Should I backoff and schedule an Alarm to try again?
Can you think of any other pitfalls with this?
1) I would fire the C2DM registration as soon as possible. Nothing in particular, but since the request is asynchronous, firing it up early will help me get the reg ID sooner. However, no need to fire the registration each time the app starts. Once is sufficient.
2) Whenever Google decides to update the reg ID it will send it to the device and you need to do the same steps you followed when you receive the reg id for the first time i.e. convey it to the server.
3 & 4) You may want to go through this documentation. It stresses the fact that you need to make it sure that you send the registration ID to your server and keep on trying. I assume here that if the reg ID is refreshed, and your server still has the old ID, it will not be able to send messages to the device. It will receive a 200OK with an Error Code of InvalidRegistration which means a (missing or) bad registration id.
5) Cannot comment much - would say that it depends on the design of your application. But one thing worth noting is that C2DM is still in Beta so expect things to be different in the long run.
Try to prompt the user with a choice of google accounts that are already on the phone. The http://code.google.com/p/chrometophone/source/checkout shows this. Look at SetupActivity.java for getGoogleAccounts(), etc.

Categories

Resources