Back to Top

Send push notifications from your server through GCM

Updated 11 February 2016

In this age of smartphones, the android app plays a great role for your e-commerce store. In order to notify the customers about the latest featured products and categories, the notifications seem to be a required option. The notifications in the android app can send in two ways: pull and push.


In pull notification, the app hits the server and gathers the data while in push notification, the server sends the data by its own. Although, the pull notification is very easy to implement but pulling consumes a lot of battery and reduces the battery life as it requires constant connection with the server.


Due to some of these disadvantages, the push notification comes into light. The push notification can be easily implemented using GCM(Google Cloud Messaging). The GCM is a free service that allows developers to send data from third party servers to their applications running on android devices.

Start your headless eCommerce
Find out More


In order to send the notification from the server, we need to generate a GCM authorization key (needed on the server side for sending the messages to google cloud), sender ID/ project ID (required in the app to getting the token from GCM server and register it with your server.

For these, you have to register your application on the google server


You can send the notification to all the apps using the token ‘global’.

The code underneath will describe other things very well.

// message to be send to the app
$message = array(					
	'title'		=> 'Demo Notification',
	'message'	=> 'Demo Message Demo Message'

// Set POST variables
$url = '';

$topic = 'global';// global topic will send notification to all the apps with the same authorization id

// the data will be send in the array below
$fields = array(
	'to'			=> "/topics/".$topic,
	'data'			=> $message,
	'time_to_live'	        => 30,
	'delay_while_idle'	=> true

$auth_key = 'AIzaSyDXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // authorization key of the app

$headers = array(
	'Authorization: key='.$auth_key,
	'Content-Type: application/json'

// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// Disabling SSL Certificate support temporarly
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

// Execute post
$result = curl_exec($ch);
if ($result === FALSE) {
	die('Curl failed: ' . curl_error($ch));

// Close connection

. . .

Leave a Comment

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

Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home