Hello Developers,
If you want to send push notification from your php web services to an Android Phone then copy below function in your php file
<?php
function AndroidPushNotification($deviceToken, $parameter1, $parameter2) {
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array (
'registration_ids' => array (
$deviceToken
),
'data' => array (
"key1" => $parameter1,
"key2" => $parameter2,
)
);
$headers = array (
'Authorization: key=AIzaSyDBQ0JGlQgh9LcXiQrOLNnzdMt7BKopPKM',
'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 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode ( $fields ) );
// Execute post
$result = curl_exec ( $ch );
// Close connection
curl_close ( $ch );
}
?>
Other argument you can use as your developers choice and pass it in array keyd "data" and that will send to Android phone by Google.
GCM (Google Cloud Messaging Server) also required a authorization header that uses "Authorization: key=". This key you can get by register an app in Google developer console and provide server key in it.
Then you can send this from anywhere and also its rquired CURL on in your server and if you want to debug this print $result variable and this will return result by GCM.
If you want to send push notification from your php web services to an Android Phone then copy below function in your php file
<?php
function AndroidPushNotification($deviceToken, $parameter1, $parameter2) {
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array (
'registration_ids' => array (
$deviceToken
),
'data' => array (
"key1" => $parameter1,
"key2" => $parameter2,
)
);
$headers = array (
'Authorization: key=AIzaSyDBQ0JGlQgh9LcXiQrOLNnzdMt7BKopPKM',
'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 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, json_encode ( $fields ) );
// Execute post
$result = curl_exec ( $ch );
// Close connection
curl_close ( $ch );
}
?>
Function have following parameters :
$deviceToken : Every Android phone have unique device id / device token. So tell your developer to send this token in time of registration and save it in your user table. When you want to send push notification to some user then fetch this device id from user table and pass this in as first argument of function.Other argument you can use as your developers choice and pass it in array keyd "data" and that will send to Android phone by Google.
GCM (Google Cloud Messaging Server) also required a authorization header that uses "Authorization: key=". This key you can get by register an app in Google developer console and provide server key in it.
Then you can send this from anywhere and also its rquired CURL on in your server and if you want to debug this print $result variable and this will return result by GCM.
Comments
Post a Comment