Skip to main content

Android Push Notification PHP class using Google Cloud Messaging (GCM)

Google cloud Messaging (GCM) is a service that helps developer to send push notification data to any Android devices. With help of this feature developer can send push message to Android device whenever new data or promotion offers arrives in Android application.

Today lots of Android applications using Google cloud Messaging service to send multiple push messages to multiple Android devices. You can find more at it’s official document at https://developers.google.com/cloud-messaging/

Push Notification flow with PHP Web Server APP and GCM

android_push

  1. Android device send Sender Id and application Id to GCM server.
  2. GCM server generates unique registration Id and sends them back to Android device.
  3. Android device sends registration Id to PHP web server application.
  4. PHP web server stores registration Id to Database for later use.
  1. When server needs to send Push Message, It fetch previously stored registration Id from Database
  2. And send them to the GCM server along with the push message data.
  3. GCM server will send that message to particular Android device using device registration Id

 

Get Google Cloud messaging API key

First you need to register your application into Google api Console at https://console.developers.google.com/iam-admin/projects

 

create project in Google

 

After creating your project find Google Cloud Messaging under Google APIs > Mobile APIs and enabled it.

google_cloud_message

Now go to Credentials and navigate to Where will you be calling the API from? dropdown and select option, for server we will choose Web server from the dropdown.

Google dropdown

 

Click on Create an API key button that will generate an API key, copy this key and paste it at safe place, we will use this key in our Android push notification PHP class.

create_api_key

 

Android Push Notification PHP Class

Create a php file called it androidNotification.php and add following code.

Copy the Google API key which we have just created in above step, and paste it where I mentioned GOOGLE_API_KEY. This key is authorizing our Class to send a message to Google Cloud server, and instantly cloud server sent it to Android device as Push messages.

Add following Public function to the class.

 

This function is responsible to send notification message to Google’s GCM server url with Android device registration id via curl, (Note: we need Android device registration id from Android phone).

 

Wrapping up whole Class file

Push Notification Call

Include the above Android Notification Class to the file from where you want to call notification.

include_once 'androidNotification.php';

Call the notification class and add your device registration id where I wrote YOUR_DEVICE_REGISTRATION_ID .

You can send one message to multiple devices with this class, you just need to pass your device registration ids array like

$result = $sendAndroidPush->sendNotificationAndroid(array($registatoinId, $registatoinId2, $registatoinId3,… and so on, $data);

Wrapping notification call

Conclusion

You can download complete Push Notification PHP class from above Link, remember you still need android device registration id to send your message. Let me know your thoughts in comment below.

Hardik Joshi

Hardik Joshi is Founder, Developer and Writer at eCodezone.com, He loves to write usable code for the Web and other digital sources.

  • Khurshid Alam

    Nice, but it didn’t explain how do I get my device registration id? All tutorials are javascript tutorials and very complicated and required to install Android Studio.

    • You have to install android studio or eclipse editor and also have to code separately in android to fetch registration id.
      You can only get registration id from android device.