Google Cloud Messaging is a service provide by google to send push notification on android device. GCM is used to send new data from server to android application on device. Adding GCM to your android application increase your user experience help you to send latest data from server. In this post i will explain you how to create a GCM enable android application. I am using Android Studio to develop this application.

Getting Started with GCM and PHP

In this post i am using PHP to store GCM registered id of application on server and with this id we can send notification from server to android device.

Setting up GCM

1. Open the google api console page and click on create project.

2. Go to Enable Api Google Cloud Messaging for Android and set its status as ON.
3. Get the Project Number, which will be used as sender id.

4. Click on Api & auth on right side then click on Credential link inside it and create a  Key for server applications leave field empty to allow all ip.

5. Now get Api Key for server application.

GCM Server Application in PHP

I create a simple application to make my app work i save GCM Registered Id received from android application in a text file and when i need to send something i get ID from text file and send notification to application using GCM. You save that id into your database table and send notification to multiple user by using their unique GCM register id.

Android GCM application

For this application i am using Android Studio.

Start your New Android Project

1. Create a new project in Android Studio from File ⇒ New ⇒ Android Application Project .

2. Add GCM.JAR file to provide your app GCM support. Download GCM.JAR file and copy that file into your libs folder { app ⇒ libs ⇒ gcm.jar }. Now after adding jar right click on jar file and click on Add as library

3. Now open your AndroidManifest.xml file under app ⇒ src ⇒main. Add permission to your manifest file

INTERNET – To make your app use internet services
ACCESS_NETWORK_STATE – To access network state (used to detect internet status)
GET_ACCOUNTS – Required as GCM needs google account
WAKE_LOCK – Needed if your app need to wake your device when it sleeps
VIBRATE – Needed if your support vibration when receiving notification

 AndroidManifest.xml

 4. Now first we need to get GCM register ID and save that id in device using Shared preference and also send to server. Through which we can send Push Notification to device. I create to Button through which i send first get my GCMregisterId and after i get that i send it to server.

Config.java

MyActivity.java

 Activity Layout

 5. Now after saving appId we also create a broadcast and receiver to receive the notification.

GcmBroadcastReceiver.java

 GCMNotificationIntentservice.java

In this class we get get notification and display notification on device

You can download this complete application from above download link.