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.

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).


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="" />
<permission android:name="" android:protectionLevel="signature" />
<uses-permission android:name="" />

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.

   android:permission="" >
       <action android:name="" />
       <category android:name="" />

<service android:name=".GcmIntentService" />
<meta-data android:name=""
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 {
   public void onReceive(Context context, Intent intent) {
       ComponentName comp = new ComponentName(context.getPackageName(),
       startWakefulService(context, (intent.setComponent(comp)));

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() {
   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());
private void sendNotification(String msg) {
NotificationManager mNotificationManager = (NotificationManager)
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle()


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

Submit a Comment

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