Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
//## Function to send GCM push notification
function sendMessageUsingGCM($registatoin_ids, $message) {
//## Google cloud messaging GCM-API url
$url = '';
$fields = array( 'registration_ids' => $registatoin_ids, 'data' => $message,);
//## Update your Google Cloud Messaging API Key
define("GOOGLE_API_KEY", "Place Google Api Key Here");
$headers = array('Authorization: key=' . GOOGLE_API_KEY,'Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
return $result;
$id = 'device_id';//### Place Device ID here
$gcmRegIds = array($id);
$message = $_REQUEST['message'];
$message = array("gcm_message" => $message);
$pushStatus = sendMessageUsingGCM($gcmRegIds, $message);
echo $pushStatus;
Test Example :
Installation Client
Step 1:
place 'manishkprgcmlibrary' downloaded library inside you project root directory
Step 2:
Add the followings dependencies to you build.gradle file
dependencies {
compile project(':manishkprgcmlibrary')
compile ''
Step 3:
Add following permissions to your AndroidManifest.xml File
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<!-- Create Custom Permission '' replace with your project package -->
<permission android:name="" android:protectionLevel="signature" />
<!-- GCM Permissions Start here -->
<uses-permission android:name="" />
<uses-permission android:name="" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Step 4:
Add Broadcast and Service to your AndroidManifest.xml File
<!-- Register Broadcast receiver -->
android:permission="" >
<action android:name="" />
<action android:name="" />
<category android:name="" /> <!-- replace it with your package name -->
<!-- Register Service -->
<service android:name="" />
Step 5:
//### For Registering your device
new GCMRegister(this, "Your Project Number",callBack).execute(); //## ForActivity
new GCMRegister(getActivity(), "Your Project Number",callBack).execute(); //## For Fragment
//### You Can Get GCM ID By following Call Back
GCMRegisterCallBack callBack = new GCMRegisterCallBack() {
public void getGCMRegister(String id, boolean isRegister) {
Log.i("ID ",id);
Step 6:
For Receiving Messages Add Broadcast to your Fragment or Activity
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String msg = intent.getExtras().get(AppConfig.GCM_MESSAGE).toString();
Log.i("Message Received ",msg);
protected void onResume() {
registerReceiver(broadcastReceiver, new IntentFilter(AppConfig.GCM_BROADCAST_ACTION));
protected void onDestroy() {
That's It
For More Information See The Example Inside This Library
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.