Updated 15 December 2016
Earlier the polling mechanish is used to get the notifications on your android application. But that method has disadvantages like load on network and reduced battery life.
Polling Mechanism: In this method the application contacts (or hits) server or cloud (whatever the case is) to get the new notifications. This increases the network usage and reduces the battery life. And also sometimes there is no new message so all the efforts go in vain.
Push Mechanism: In this approach the server/ cloud itself connect to the application device whenever there is a new notification available. So it had saved the resorces. But the process is extremely typical one have to deal with queues, saving the notifications for resending at the time of offline device. But to made all these things easy we can use GCM.
Google Cloud Messaging:
Google provides an easy way to implement push notifications called GCM. GCM sends data from your server to your users’ devices, and receive messages from devices on the same connection. The GCM service handles all aspects of queueing of messages and delivery to client applications, and it is completely free.
GCM acts as a mediator between your server and client’s application. A full GCM implementation requires both a client implementation and a server implementation. I presume that you had already implemented the server implementation. The client implementation of GCM is extremely simple, you just have to fallow the below mentioned steps:
Step 1: Go to http://console.developers.google.com and register your project over there. On successful registration you will get
Step 2: Now add some support in your gradle files as:
1 |
classpath 'com.google.gms:google-services:1.5.0-beta2' |
1 2 3 4 5 6 |
apply plugin: 'com.google.gms.google-services' dependencies { compile "com.google.android.gms:play-services:8.3.0" } |
Step 3: Add permissions as well as sevices and recievers to your app manifest file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<permission android:name="<YOUR Package Name>.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="<YOUR Package Name>.permission.C2D_MESSAGE" /> <application //... > //... <receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="com.example.gcm" /> </intent-filter> </receiver> <service android:name=".<YOUR EXTENDED GCMLISTENER SERVICE>" android:exported="false" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> </intent-filter> </service> <service android:name=".<YOUR InstanceIDListenerService>" <!--MyInstanceIdListenerService--> android:exported="false"> <intent-filter> <action android:name="com.google.android.gms.iid.InstanceID" /> </intent-filter> </service> <service android:name=".<YOUR REGISTRATION SERVICE>" <!--RegistrationIntentService--> android:exported="false" > <intent-filter> <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> </intent-filter> </service> //... </application> |
Step 4: For each user GCM generates a token (registration id). It is the unique id of the user through which the notification is sent to the clent. So each user have to register itself with GCM using your applications senderID. Add a boolean value to preference to determine that the to0ken is generated or not. If not then the app will generate a token n send it to your server. To do this extend IntentService class and register your app.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.example.GCM; import android.app.IntentService; import android.content.Intent; import android.content.SharedPreferences; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import com.google.android.gms.gcm.GcmPubSub; import com.google.android.gms.gcm.GoogleCloudMessaging; import com.google.android.gms.iid.InstanceID; public class RegistrationIntentService extends IntentService { private static final String TAG = "RegistrationService";//ant tag you want SharedPreferences sharedPreferences; public RegistrationIntentService() { super(TAG); } @Override public void onHandleIntent(Intent intent) { sharedPreferences = PreferenceManager.getPreference(); try { InstanceID instanceID = InstanceID.getInstance(this); String token = instanceID.getToken(<YOUR SENDER ID>, GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); sendRegistrationToServer(token); sharedPreferences.edit().putBoolean("tokenSend", true).apply(); } catch (Exception e) { Log.d(TAG, "Failed to complete token refresh", e); sharedPreferences.edit().putBoolean("tokenSend", false).apply(); } Intent registrationComplete = new Intent("registrationComplete"); LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete); } private void sendRegistrationToServer(String token) { //send your token to your server. //implement the method. } } |
Your app or the Instance ID service may need to refresh tokens in the event of a security issue or when a user uninstalls and reinstalls your app during device restoration. Your app must implement a listener to respond to token refresh requests from the Instance ID service. This is implemented by YOUR InstanceIdService.
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.example.GCM; import android.content.Intent; import com.google.android.gms.iid.InstanceIDListenerService; public class MyInstanceIDListenerService extends InstanceIDListenerService { @Override public void onTokenRefresh() { Intent intent = new Intent(this, RegistrationIntentService.class); startService(intent); } } |
Step 5: Now just implement the listeners / recievers and you are ready to send or recieve your messages using GCM.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
1 comments