Android Push Notifications using GCM

“Google Cloud Messaging for Android (GCM) is a service that helps developers send data from servers to their Android applications on Android devices”. Using this service you can send data to your application whenever new data is available instead of making requests to server in timely fashion. Integrating GCM in your android application enhances user experience and saves lot of battery power.

GCM
Google Cloud Messaging GCM API

  • We can use GCM as a notification engine. On an event it will send a notification to an android application and vice-versa.
  • A notification is a small piece of information. Using GCM, maximum 4kb of payload can be sent in a notification.
  • For now GCM is a free service – lets use responsibly 🙂
  • Android App can receive the message from Google cloud messaging server (GCM) even if the app is not running via Intent broadcasting (our example app will demonstrate this).

Manifest

Let’s start with our manifest. We’ll want to add some permissions under the manifest markup.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="your.package.name.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<uses-permission android:name="your.package.name.permission.C2D_MESSAGE" />

Next step is to register our Broadcast Receiver and Service. You should already have that last line in your manifest if you added the google play services properly.

<receiver
   android:name=".GcmBroadcastReceiver"
   android:permission="com.google.android.c2dm.permission.SEND" >
   <intent-filter>
       <action android:name="com.google.android.c2dm.intent.RECEIVE" />
       <category android:name="your.package.name" />
   </intent-filter>
</receiver>

<service android:name=".GcmIntentService" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

Broadcast Receiver

We are going to start by creating a new class which extends WakefulBroadcastReceiver.

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {
       ComponentName comp = new ComponentName(context.getPackageName(),
               GcmIntentService.class.getName());
       startWakefulService(context, (intent.setComponent(comp)));
       setResultCode(Activity.RESULT_OK);
   }
}

The Service

A broadcast receiver by design cannot execute long tasks. It must call other classes to do its work. We need to create a new class. We cannot start a new activity because we do not want the user to be forced to switch to our app. Instead we use a service. It will be able to execute tasks in the background.

public class GcmIntentService extends IntentService {
   public GcmIntentService() {
       super("GcmIntentService");
   }
   @Override
   protected void onHandleIntent(Intent intent) {
       Bundle extras = intent.getExtras();
       GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
       String messageType = gcm.getMessageType(intent);
       if (!extras.isEmpty() && GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {     
  sendNotification("Received: " + extras.toString());
       }
GcmBroadcastReceiver.completeWakefulIntent(intent);
   }
private void sendNotification(String msg) {
NotificationManager mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}

logo

Value creation through technology & innovation

Patterns7 Technologies PVT LTD is software Development Company based in United Kingdom & India.The world is changing so fast that it is becoming increasingly difficult to plan ahead. We use Innovative Solutions to help businesses to get ready for the future by providing Innovative Technology Services. With deep and wide experience in providing end-to-end solution we are helping our clients to deliver Value.

Currently we are working with healthcare, manufacturing & logistics industry to bring in efficiencies in their businesses by providing real time tracking, geo-fencing & GPS technology.

Please feel free to contact us on info@patterns7tech.com

Submit a Comment

Your email address will not be published. Required fields are marked *