Saturday, 24 August 2013

Send push notifications with Cordova / Phonegap GCM from server without node-gcm

Send push notifications with Cordova / Phonegap GCM from server without
node-gcm

I'm currently working on a similar project but I was wondering if it i
necessary to use the node-gcm interface for communication with registered
devices. Because i'm trying to send the messages from a remote server
without node.js support so the interface can't be installed on the remote
server. If it is possible what's the wright structure to send messages.
<?php
class GCM {
//put your code here
// constructor
function __construct() {
}
/**
* Sending Push Notification
*/
public function send_notification($registatoin_ids, $message) {
// include config
include_once './config.php';
// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registatoin_ids,
'message' => $message,
'title' => 'test',
'msgcnt' => '3');
$headers = array(
'Authorization: key=' . GOOGLE_API_KEY,
'Content-Type: application/json',
'Content-Length: ' . strlen(json_encode($fields))
);
// 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_CUSTOMREQUEST, "POST");
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
curl_close($ch);
echo $result;
}
}
?>
When the message is send from the server I get the following message on my
Android device: "MESSAGE -> MSG: undefined" and "MESSAGE -> MSGCOUNT:
undefined"
Thanks in advance

No comments:

Post a Comment