Push Notifications using GCM in Android Studio

According to Google’s documentations “Google Cloud Messaging (GCM) is a free service that enables developers to send messages between servers and client apps. This includes downstream messages from servers to client apps, and upstream messages from client apps to servers”. The GCM service handles all aspects of queueing of messages and delivery to the target Android application running on the target device.

gcm

Source : Google

Creating API key

1.Go to Google Developer Console and select Get a configuration file.

gcm

2.Create new app or select from your existing app. write your package name also and click on continue to choose and configure services.

gcm

3.Select cloud messaging and click on Enable Google Cloud Messaging.

gcm

4.Note down generated API key and sender id, click on Continue to generate configuration file.

5.Click on Download google-services.json and save it for later use.

gcm

Creating Android Project for GCM

1.Create a new android project in your Android Studio and add dependency for GCM.

  1. Add a dependency to your project level build.gradle
  2. Add a plugin to your app level builg.gradle
  3. Add a dependency to your app level build.gradle

2.Put downloaded configuration file google-services.json inside app folder.

gcm

3.Add permissions in manifest.xml

4.Define GCM services in manifest.xml

5.Before generating GCM device id, check whether Gooogle Play Service is updated or not.

6.Now generate GCM device id by writing following code in seperate thread apart from main thread.

7.Now calling this method from your onCreate() will give you device token

8.Create GCMService which will be called when receive any notification.

gcm

Download full source code from GITHUB.

ANDROID OS BATTLE : ANDROID L VS ANDROID M
AutoCompleteTextView in Android