Overview of Google Cloud Messaging
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”
- Send a request to GCM from the device to register (with the google project number).
- GCM sends back the device registration ID.
- Device sends the device registration ID to the provider.
- Provider sends a message to the GCM (with API key and the device registration ID).
- GCM sends the message to the device.
Downloading the SDK
- Use this url to download the wso2GCM.jar
Creating Google project
- Go to https://console.developers.google.com and create an account.
- Now create a new project.
- Then click on Enable and manage API’s and enable Google Cloud Messaging for Android API for the project.
- Then click on Go to credentials and you can get your API keys from there. These keys will be needed to send push notifications from the server. The API key will be not needed to register devices for push notifications. But Project number is needed to register devices.
- You can get the google Project Number from the project home.
Creating Android Project
- Create a new android project by providing necessary details.
- Import the downloaded library into the project
- After creating the project open the AndroidManifest.xml and add the following permissions.
INTERNET – To make your app use internet services
ACCESS_NETWORK_STATE – To access network state (used to detect internet status)
GET_ACCOUNTS – Required as GCM needs google account
WAKE_LOCK – Needed if your app need to wake your device when it sleeps
VIBRATE – Needed if your support vibration when receiving notification
Also you need to add some broadcast receivers as mentioned below.
Final AndroidManifest.xml will look like this.
In the above code change the org.wso2.pushnotifications with your package name
- Now create a class called GCMIntentService.java. This class handles all the GCM related services. GCMIntentService class will look like this.
- Now all the hard work is done. You can use the following method provided in the library to do the rest of the work. (Please note that parameters are not mentioned here)
GCMRegistrar.register – To register the device.
GCMRegistrar.getRegistrationId – To get the device registration ID.
GCMRegistrar.isRegisteredOnServer – To check whether the device is registered on your server.
- Now you need to store the devices IDs some where. This library provides a class called ServerRegistrationUtilities.java and using this class you can do that part very easily. Following methods can be used to register on the server.(Please note that parameters are not mentioned here)
ServerRegistrationUtilities.register – To register on server.
ServerRegistrationUtilities.unregister – To unregister from the server.
*Please note that the url patterns should be like this.
…………………../register for registration.(DeviceId will be passed)
……………../unregister for unregistration. (DeviceId will be passed)
Send a test notification
- You can send a push notification from your server using the google API key or just use an online tester.
- You can go to apns-gcm.bryantan.info and check your android application easily.