GCM Client app implementation for Android.
For a complete tutorial on how you can make use of this project visit this link. #Features:
- Client app will register with GCM using an intent service.
- It will provide users the ability to retry/refresh the registration/un-registration with GCM server.
- It will implement registration/un-registration to your own server using an exponential back-off (a retry mechanism).
- It will use shared preferences to store some necessary information.
- By default, if a previous registration id is available, it will use that id from shared prefs instead of a fresh retrieval.
- The unregister button will unregister the id from both GCM server and your own notification server.
- The refresh button will retrieve a fresh id from GCM server and register it to your own notification server.
- It will be able to handle multiple types of notifications and trigger different events accordingly.
#Java Classes at a glance:
- GCMCommonUtils: This class includes some commonly used variables and methods.
- GCMSharedPreferences: This class holds the registration id in shared preferences and also some other values used throughout the project.
- MyGcmListenerService: It's an intent service which runs in the background (always) for the project and responsible for getting the notification and triggering different events accordingly.
- MyGCMRegistrationIntentService: It's another intent service which is responsible for registering/un-registering the device with both GCM and the notification server. It detects whether to register or un-register by evaluating the Extra value put for the intent service with
putExtra()
method. - MyInstanceIDListenerService: An extra class which is not used in the sample project but provided for further digging. You can make use of it if you want. It can be used to handle token refresh (I am using a different but easier method to refresh tokens).
- PushNotificationMainActivity: This is the main activity. Includes a
BroadcastReceiver
to handle messages from the intent serviceMyGCMRegistrationIntentService
. - ScrollingActivity: A dummy activity to show an example of handling multiple types of notifications.
#Java methods at a glance:
checkPlayServices(Activity activity)
: Check play services API availability and install/update if necessary. Defined in GCMCommonUtils class.onMessageReceived(String from, Bundle data)
: Override method in MyGcmListenerService. Handles messages received through push notification.sendNotification(String message, String type)
: Generates notification on the device with the message and triggers an event if clicked from the notification area in device according to the type specified.onHandleIntent(Intent intent)
: Override method in MyGCMRegistrationIntentService, handles registration intent, registers or unregisters with GCM and server.registerWithServer(String token, InstanceID instanceID)
: Defined in MyGCMRegistrationIntentService. Sends the token and the instanceID (string) to server in order to register with the server. It employs an exponential backoff mechanism to retry in case of failure.unregisterFromServer(final String regId)
: Defined in MyGCMRegistrationIntentService. Sends the token/regid to server and unregisters the device for the server. It employs an exponential backoff mechanism to retry in case of failure.post(String endpoint, Map<String, String> params)
: Defined in MyGCMRegistrationIntentService. It triggers an HTTP post request.startRegistrationService(boolean reg, boolean tkr)
: Defined in PushNotificationMainActivity. Ifreg
istrue
, it tries to register, iffalse
it tries to un-register. Iftkr
istrue
, a fresh token is retrieved, otherwise old token from shared preferences is used.