When a 3rd-party server posts a message to GCM and receives a message ID back, it does not mean that the message was already delivered to the device.
Rather, it means that it was accepted for delivery. What happens to the message after it is accepted depends on many factors. In the best-case scenario, if the device is connected to GCM, the screen is on, and there are no throttling restrictions see Throttlingthe message will be delivered right away. Otherwise, it will be stored in the GCM servers until the device is awake. However, if the collapse key is not set, both the new and old messages are stored for future delivery.
Note: There is a limit on how many messages can be stored without collapsing. That limit is currently If the limit is reached, all stored messages are discarded. Then when the device is back online, it receives a special message indicating that the limit was reached.
The application can then handle the situation properly, typically by requesting a full sync. If the device is not connected to GCM, the message will be stored until a connection is established again respecting the collapse key rules.
If the device never gets connected again for instance, if it was factory resetthe message will eventually time out and be discarded from GCM storage. Finally, when GCM attempts to deliver a message to the device and the application was uninstalled, GCM will discard that message right away and invalidate the registration ID.
Future attempts to send a message to that device will get a NotRegistered error. See How Unregistration Works for more information.
Subscribe to RSS
Although is not possible to track the status of each individual message, the Google APIs Console stats are broken down by messages sent to device, messages collapsed, and messages waiting for delivery.
To prevent abuse such as sending a flood of messages to a device and to optimize for the overall network efficiency and battery life of devices, GCM implements throttling of messages using a token bucket scheme.
Messages are throttled on a per application and per collapse key basis including non-collapsible messages. Each application collapse key is granted some initial tokens, and new tokens are granted periodically therefter.
Each token is valid for a single message sent to the device. If an application collapse key exhausts its supply of available tokens, new messages are buffered in a pending queue until new tokens become available at the time of the periodic grant.
Thus throttling in between periodic grant intervals may add to the latency of message delivery for an application collapse key that sends a large number of messages within a short period of time.
Messages in the pending queue of an application collapse key may be delivered before the time of the next periodic grant, if they are piggybacked with messages belonging to a non-throttled category by GCM for network and battery efficiency reasons. Whenever the application receives a com. If the server fails to complete the registration, it should try again or unregister from GCM.
When an application is updated, it should invalidate its existing registration ID, as it is not guaranteed to work with the new version. Because there is no lifecycle method called when the application is updated, the best way to achieve this validation is by storing the current application version when a registration ID is stored.
Then when the application is started, compare the stored value with the current application version. If they do not match, invalidate the stored data and start the registration process again. Similarly, you should not save the registration ID when an application is backed up.
This is because the registration ID could become invalid by the time the application is restored, which would put the application in an invalid state that is, the application thinks it is registered, but the server and GCM do not store that registration ID anymore—thus the application will not get more messages. On the server side, as long as the application is behaving well, everything should work normally. However, if a bug in the application triggers multiple registrations for the same device, it can be hard to reconcile state and you might end up with duplicate messages.
GCM provides a facility called "canonical registration IDs" to easily recover from these situations. A canonical registration ID is defined to be the ID of the last registration requested by your application.Before getting started, keep in mind:. To prepare for migration, you can review a video tutorial that walks through the same basic steps described in this guide. If you already have an existing Firebase project into which you would like to migrate a GCM app and its users, see Migrate GCM to an existing Firebase project for important alternative instructions.
In the Firebase consoleselect Add Project. A new google-services. Select Continue and follow the detailed instructions for adding the Google Services plugin in Android Studio. Make sure to remove the following obsolete and potentially harmful, as they may cause message duplication elements from your app's manifest:.
Note that the new FCM version of gcm-http. Depending on which GCM features your app uses, you may have additional migration tasks to perform. Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4. For details, see the Google Developers Site Policies.
The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Since GCM keeps getting updated, most of the resources I have searched seem outdated or unclear.Android tutorial: Push notification using Google Cloud Messaging (GCM) - Part 1
Basically, I am confused over when the tokens and ID's expire. For reference, I am working with Android.
I'm already a bit confused here. The InstanceID is assigned the moment my app goes online? Does it ever change? What about when the app upgrades or is uninstalled and reinstalled or the device is restored? By calling InstanceID. Is there any value to storing the string you retrieve by calling getID?
The docs seem to indicate that you actually retrieve a new InstanceID when you call getIDso that complicates things even more. My app server stores this Token, and can use this to send messages to the GCM servers, which will then send the message to the device.
The device uses the stored InstanceID to actually receive these messages, I believe. So having a class that extends GcmListenerService will allow me to receive these messages with onMessageReceived? I don't have to do anything special other than defining it in the AndroidManifest? I don't have to actually tell it to use the InstanceID? It just magically knows?
When do these ID's and Tokens expire? Do they expire? I store the Token as a string on the server, but if at any point one of these expires, how do I know they have expired?
I can always generate a new InstanceID and Token, that much seems easy, but then do the old ones stay active?The instructions in this page assume that you have completed the steps for adding Firebase to your Android project.
FCM clients require devices running Android 4. Note that you are not limited to deploying your Android apps through Google Play Store. If you want to target single devices or create device groups, you'll need to access this token by extending FirebaseMessagingService and overriding onNewToken. This section describes how to retrieve the token and how to monitor changes to the token.
Because the token could be rotated after initial startup, you are strongly recommended to retrieve the latest updated registration token. When you need to retrieve the current token, call FirebaseInstanceId.
The onNewToken callback fires whenever a new token is generated. After you've obtained the token, you can send it to your app server and store it using your preferred method. It is recommended to do this in two places: in the main activity's onCreate method, and in its onResume method.
The check in onCreate ensures that the app can't be used without a successful check. The check in onResume ensures that if the user returns to the running app through some other means, such as through the back button, the check is still performed. If the device doesn't have a compatible version of Google Play services, your app can call GoogleApiAvailability. When an Instance ID is generated, the library will upload the identifier and configuration data to Firebase. If you prefer to prevent Instance ID autogeneration, disable auto initialization for FCM and Analytics you must disable both by adding these metadata values to your AndroidManifest.
After the client app is set up, you are ready to start sending downstream messages with the Notifications composer. This functionality is demonstrated in the quickstart samplewhich you can download, run, and review. To add other, more advanced behavior to your app, you can declare an intent filter and implement an activity to respond to incoming messages.
Push Notifications Tutorial for Android using Google Cloud Messaging (GCM)
For details, see the guides for sending messages from an app server:. Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4. For details, see the Google Developers Site Policies.Google Cloud Messaging GCM is a service that enables developers to send data from servers to both Android applications or Chrome apps and extensions.
It forwards notifications of third party applications to the Apple devices; such notifications may include badges, sounds or custom text alerts. Push messages sent to GCM always returns a reply to the application server informing whether or not the delivery of the message to GCM was successful, and this reply can tell the application server if a device ID has changed or if it has become invalid and can no longer receive push messages.
Note that none of the providers can or will tell you if the message actually got delivered to the device, they merely tell you whether or not the message was accepted for delivery.
The primary reason for this is that a device may be unavailable e. The raw contents and interpretations of the push messages are probably the biggest differences between the GCM and APNs:.
Where GCM simply passes raw JSON data to a background service from your app, free for the app to process whether it is in the foreground, background or not even started. It is totally up to you. This is all done by iOS. If it is in the foreground, you receive the parameters for the alert, badge and sound in your code along with an arbitrary JSON payload, where you can do whatever you want with it.
Notice, however, that there is limitations on the size of the payload in the messages. It is better to notify your app that something has changed than to send along the change in the push message which is practically impossible for real life data.
Both platforms allow for sending messages from one application server to multiple devices, and for multiple application servers to send to the same device.
Migrate a GCM Client App for Android to Firebase Cloud Messaging
Lastly, both platforms allow for an arbitrary JSON object to be sent from your application server to the device, though the maximum allowed size of this object differs more on that later.
These are, in my opinion, the most important differences and similarities between Google Cloud Messaging for Android and Apple Push Notification Service. It is intended to give an overview of the differences, so you get a better understanding of how to implement your cross-platform push message solution. What is Google GCM? Differences in the contents of the push messages The raw contents and interpretations of the push messages are probably the biggest differences between the GCM and APNs: Where GCM simply passes raw JSON data to a background service from your app, free for the app to process whether it is in the foreground, background or not even started.
The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I am confused on relationship between registration id and tokens. In the tutorial for GCM from Google, we register for a registration id in the beginning.
However, we also get a token.
Subscribe to RSS
Now, in the diagrams, we send the registration id to the targeted server. However, do we also send the token? I know that the token is derived from the registration id. Is the token used as an authentication mechanism between GCM and the app and the server never knows about the token? You can read more at the following questions, one of them you have read before with one of your previous questions:.
Each InstanceID can issue several tokens. These tokens are used to identify the InstanceID and can expire and be refreshed. On the client device, you initialize an InstanceID, then with that InstanceID you generate a token registration token.
You send that token to your server, which uses the token to send messages to the InstanceID installed application. If that token is invalidated for any reason like the application is uninstalled or the token is compromised, a new token should be generated and sent to your server.
Learn more. Asked 4 years, 5 months ago. Active 3 years, 11 months ago. Viewed 19k times. Active Oldest Votes.Sound interesting? Read more about this series and their efforts in our introductory series post. In that scenario, it is often useful to notify the client of certain events such as when a new message has been received on the serverand it is convenient to have a way for the server to communicate this information to the client without requiring the client to request it.
This type of communication is called a push notification. This push notifications tutorial assumes that Android Studio is used as the IDE, with a target device running Android 4. GCM is a service provided by Google that helps developers implement push notifications in their applications. By using GCM, developers are not required to implement their own method for sending data from their server to the client applications.
Both the app server and Android client need to register with GCM and provide information to uniquely identify and authorize them. The project number is the sender ID that you will use to register in your client application.
The following screen will allow you to create the key, which should then be displayed on the Credentials page. Use this key in your server application to communicate with GCM.
For more information, see blog post for implementing push notifications on an application server. As an alternative to hard coding the sender ID project number in your application, you can use Google Developers Console to generate a configuration file, and use the Google Services plugin for Gradle to get that information into your project.
To do so, follow these steps:. Instead of the above steps, you could simply hard code the sender ID into your source code when you register the device with GCM. This is simpler, however if your application uses other Google services, importing the configuration file may be a benefit in the long run. With the SDK Tools installed, you can add a dependency to your project, for example: compile 'com.
Your application will need certain permissions:. The last permission may look a little funny. This is a special permission that is specific to your app, which you define in your manifest. If your package name is com. We can now begin coding. The first thing we need to do is register with GCM to get a registration token and provide that to our server. This token uniquely identifies an Android device, and our server will use it to send messages to that specific device.
The code to register is straightforward:. Keep in mind that the above code uses network resources and should not be run on the main thread; you should use your preferred technique for executing background tasks on Android Service, AsyncTask, etc. Once you have a token, send it to your application server to complete the registration process. Messages for our Android application are received by the GcmReceiver that we declared in our Manifest.
This is were we write code to process the messages. Here is a basic example that displays the message as a notification:. If you look back at the manifest, you will see that we declared two permissions for the GcmReceiver: com.