Push Notification Using GCM or Google Cloud Messaging

PUSH NOTIFICATION USING GCM OR GOOGLE CLOUD MESSAGING

Following is an example of implementing push notifications using GCM. GCM stands for Google Cloud Messaging using API services provided by Google.

 

It is a free service provided by Google which most application developers use to send messages and notifications to Android Devices and Chrome browser.

 

The following example is a demonstration of actual implementation using PHP and HTML/JavaScript. The example is broken into two files:

send_android_notification.php
android_notification_system.php

 

It will accept the required parameters for calling GCM service and using CURL functions, it will send the required parameters to GCM API.

 

<?php

$topic = “”;

if (isset($_GET[‘topic’])) { $topic = $_GET[‘topic’]; }

$msg = “”;

if (isset($_GET[‘msg’])) { $msg = $_GET[‘msg’]; }

$title = “”;

if (isset($_GET[‘title’])) { $title = $_GET[‘title’]; }

$sub_title = “”;

if (isset($_GET[‘sub_title’])) { $sub_title = $_GET[‘sub_title’]; }

$ticker = “”;

if (isset($_GET[‘ticker’])) { $ticker = $_GET[‘ticker’]; }

define( ‘API_ACCESS_KEY’, ‘INSERT HERE YOUR OWN API KEY’ );

$registrationIds = array( $_GET[‘id’] );

$msg = array

(

‘message’ => $msg,

‘title’ => $title,

‘subtitle’ => $sub_title,

‘tickerText’ => $ticker,

‘vibrate’ => 1,

‘sound’ => 1,

‘largeIcon’ => ‘large_icon’,

‘smallIcon’ => ‘small_icon’

);

$fields = array(

‘to’ => ‘/topics/’ . $topic,

‘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;

exit;

?>

 

android_notification_system.php

It creates a GUI to get the data required to call the above .PHP file.

<html>

<head>

<script>

function SendAndroidNotification()

{

var topic = document.getElementById(“topic”).value;

var ticker = document.getElementById(“ticker”).value;

var title = document.getElementById(“title”).value;

var subtitle = document.getElementById(“subtitle”).value;

var message = document.getElementById(“message”).value;

var url = “http://localhost/gcm/send_android_notification.php?topic=”+topic+”&msg=”+message+”&title=”+title+”&sub_title=”+subtitle+”&ticker=”+ticker+””;

alert(url);

document.forms[‘an’].action=url;

document.forms[‘an’].submit();

alert(“Android Notification Message submitted for sending.”)

}

 

</script>

</head>

<body>

<br>

 

<h2> Send Android Notifications </h2>

<br>

 

<form method=”GET” action=”” id=”an” accept-charset=”UTF-8″>

<p><label>Your Topic<strong>*</strong><br>

<input type=”text” size=”48″ name=”topic” id=”topic” value=””></label></p>

<p><label>Ticker<strong>*</strong><br>

<input type=”ticker” size=”48″ name=”ticker” id=”ticker” value=””></label></p>

<p><label>Title<br>

<input type=”text” size=”48″ name=”title” id=”title” value=””></label></p>

<p><label>Sub Title<br>

<input type=”text” size=”48″ name=”subtitle” id=”subtitle” value=””></label></p>

<p><label>Message<strong>*</strong><br>

<textarea name=”message” id=”message” cols=”48″ rows=”8″></textarea></label></p>

<p><input type=”button” name=”sendfeedback” value=”Send Android Notification” onClick=”SendAndroidNotification()”></p>

</form>

</body>

</html>

 

Both the above files should be in the same folder (public_html) on web-server where PHP is enabled with CURL module.