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
Related
I have two questions related to Android Push Notification System:
What is the working principle of this system? The client sends its own IP to the Google Cloud Messaging Service (for example when it switches its own IP)? So it a sort of pooling?.
How do you know that Google Cloud Messaging Service "looks into" the content of the notification message (created in the server and dispatched to the client)?
Answering the question about how GCM service contacts the client, the GCM client contacts GCM to create the connection. You are correct that device addresses change as the device disconnects and reconnects so GCM cannot initiate the connection from the server side.
This connection is maintained as much as possible and is not created for specific messages.
The registration id identifies the device and app and allows GCM to route the message to the device, if it is connected. If it is not connected, GCM needs to wait until the device reconnects.
What is the working principle of this system? The client sends its own
IP to the Google Cloud Messaging Service (for example when it switches
its own IP)? So it a sort of pooling?
I'm adding an image here describing how GCM works.
This is a step by step presentation. You need to get the push registration ID first when your application launches. So if you've a backend server to send some push notification in your application, you need to pass the registration ID to your backend server. So when you need to send a push notification, your backend server will send the push directly to GCM with the targeted registration ID. GCM manages to push the notification in your device when your device comes online.
So this is not any kind of pooling. The only thing GCM needs to know is the registration ID of your device when it comes online and tries to communicate with GCM. Once your device is registered, GCM sends the push notification using that registration ID.
How do you know that Google Cloud Messaging Service "looks into" the
content of the notification message (created in the server and
dispatched to the client)?
This question is not very clear to me. As far as I have understood, you wanted to know how GCM understands to whom it needs to send the push notification when the notification is coming from your backend server. If this is your question, then I think I have answered it already in the previous section of my answer.
GCM doesn't need to look into your notification content to know the destination of the push notification. As I said earlier, when your application launches, it requests for a push registration ID from GCM and when it receives an registration ID, you might have to pass the registration ID by calling a service of your backend server. The server then knows to whom it might send some notification.
So, when its time to send a notification to your client application, the backend server sends the notification to GCM with the registration ID you sent to your backend server earlier. GCM then handles sending the push notification to the client when the application comes online.
Hope that helps!
I am trying to make a simple messaging app that has a mysql and php server with an android app. My back end revolves around mysql to store and php to communicate from the database to the device and vise versa.
Now, what I am trying to accomplish is this: sending device->GCM->target device.
What I had in mind was that the database I created stores the gcm Id for all user. That way, when a user wants to send a message, their app sends a message to my database to be stored and retrieves the targets gcm Id and then sends the targets gcm id to the gcm servers to create the push notificiation. When the target receives the gcm message, it sends a response to the database to receive the actual sent message.
Is this possible, and how would I go about doing this?
I already have a gcm receiver implemented from here:
https://github.com/codepath/android_guides/wiki/Google-Cloud-Messaging
No. Don't do it.
For any internet connected device (server or mobile) to request a push to a device, it needs to send a POST request to the GCM address (https://gcm-http.googleapis.com/gcm/send) passing the server API key and the device GCM reg id which should receive the message. And having any of those values available on a device is a security risk for your application.
Having your registration ID, means ppl could easily "copy" your ID and start sending messages from their servers on your behalf.
The GCM device registraion ID, means anyone could start spaming your users, and you certainly don't want that.
You could look into GCM Upstream (https://developers.google.com/cloud-messaging/upstream) but that only means your client code will be easier, as it's as simple as calling gcm.send(String);, but you still have to handle that on your server application.
The correct way is to have on your server a table that maps userId with gcmRegId and have devices send to your server messages to their desired userId. Your server should process one device "send" message and create a push to the other device. That logic should be fairly simple on the server side, after you already have a whole chat application developed.
We are developing apps both in IOS and Android. GCM push notification has been enabled for IOS and its working fine now. The package name for both the platforms are going to be the same.
I was given SERVER API KEY and SENDER ID by ios developer to set up gcm for android. While looking for the steps, I came across https://developers.google.com/cloud-messaging/android/client.
I kept to myself that the steps listed in the contents need to be done to set up GCM for android (please correct me if I am wrong).
Get Config file and add it to Android project
Set up Google play services (I added gcm in my project dependency)
Add entries to Manifest file
Check for google play services APK
Obtain registration token.
"An Android application needs to register with GCM connection servers before it can receive messages"
"The client app should store a boolean value indicating whether the registration token has been sent to the server." - My backend team told me I dont need to send them anything I have to just configure gcm in the app and the app will receive messages from backend.
So, My question is Do I need to have RegistrationIntentService and MyInstanceIDListenerService. Also, Do I have to define my InstanceIDListenerService in Manifest?
Our backend uses device id to send push notifications to devices so they dont need registration token to be sent to them as we send device id. So in this case, Should I register my app with GCM using RegistrationIntentService and InstanceIDListenerService? if so, should the app keep the registration token with itself. Is this registration needed?
GCM supports three types of downstream (server-to-client) messaging: send to a specific device (also called "simple" or "targeted"), send to a topic, or send to a device group. Your question says, "our backend uses device id to send push notifications to devices". It is not clear what "device ID" is and which type of messaging you intend to use. Your backend team has told you that you "don't need to send them anything". If that is true, I don't know where they are getting the "device ID".
Each of the three types of messaging provided by GCM require client devices to register with GCM and obtain a registration token. To send a message to a specific device, the registration token is effectively the "device ID". So yes, you need to implement something similar to the RegistrationIntentService and InstanceIDListenerService described in the documentation.
The description in the documentation about needing to send the registration token to the App Server is misleading. That is only required for targeted messaging. The documentation for receiving topic messages states: "Note that, for topic messaging it's not required to send the registration token to your app server; however, if you do send it, your server can verify the validity of the token and get more information about the app that created it."
I'm trying to use GCM to establish communication between some users. They should send to each other their GPS location. Is it even possible? There is a subscribeToCloudMessage method but I don't know whether it will be useful or not.
Info: Server works fine (Google app-engine) with all the required keys.
It's possible for one app to send a message to another app via Google Cloud Messaging if the sending app knows the registration ID of the recipient app. Usually when a app register to Google Cloud Messaging, it sends it's Registration ID to a server, and that server sends GCM notifications. If you want the app to send the notifications directly, you must send each device (where the app is installed) the Registration IDs of other devices that it needs to send messages to.
I have read the GCM detail from Google WebSite and watched the Google IO Video.
But in the Video and GCM Documentation there is example GCM Send one same message to multiple android devices. But in my app I don't want to send the same message to multiple android device.
For Example
I have two android devices with different google account and I want to Sync Notes in app with GCM.
First device register with one#gmail.com
Second device register with two#gmail.com
First device have different Notes. and
Second device have different Notes
If I update notes on server for first device. I want it will Sync with first device(one#gmail.com) not with Second device(two#gmail.com)
Is it possible with the GCM.....? please Let me know.
Thank you
Yes, each instance of you application running on each device will have a unique registration id
Registration ID: 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.
When your application on a particular devices registers for GCM, it receives a unique registration ID. This registration ID is received after the GCM registration is successful. You can use this registration id (unique to one#gmail.com and one particular device) to send the message to that app+device combo only.
A 3rd-party application server can either send messages to a single
device or to multiple devices. A message sent to multiple devices
simultaneously is called a multicast message.
For example
Content-Type:application/json
Authorization:key=AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA
{
"registration_ids" : ["APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx..."],
"data" : {
...
},
}
the above code sends the message to one instance of your app running on a particular device