Android Push Notification Using PHP and Google Cloud Messaging

By | November 26, 2015

Sending push notification to android device is very important part of an App. Most of the time we use it for sending some information as well us for promotion. I am going to tell you how you can use PHP script to send Android Push Notification using Google Cloud Messaging. Script is so simple and you can use it easily in your project.

Things we need

  • API access key from Google API’s Console
  • Device ID

I assume that you are having both of the things  so now we will move to next part that is our PHP Script for sending the push notification.

PHP Script for Sending Push Notification


// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'Your Google API Key' );

$registrationIds = array( $_GET['id'] );
//Write your text here
$msg = array('message' 	=> 'Hello Mr xyz go to www.tellmequotes.com');
// Add your device id and message.
$fields = array('registration_ids' => $registrationIds, 'data'=> $msg);
 
$headers = array('Authorization: key=' . API_ACCESS_KEY,'Content-Type: application/json');
 
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

echo $result;

Just replace your credentials with script and run it. It will surely send the push notification to your android app. Thank you for reading PHP script for sending android push notification using Google Cloud Messaging. If you have any advice or comment please write it below.

Leave a Reply

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