Sending push notification to Android app developed in titanium (cross platform) using PHP
To send a push notification from a server to an Android app, I am using the following script.
<?php
$message = "hi there";
$apiKey = "xxxxxxxxxxxx";
$registrationIDs = array("xxxxxxx");
$url = 'https://android.googleapis.com/gcm/send';
// Set POST variables
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => $message,
)
);
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
$ch = curl_init(); // Open connection
curl_setopt($ch, CURLOPT_URL, $url );
// Set the url, number of POST vars, POST data
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); // Execute post
if($result === false)
die('Curl failed ' . curl_error());
curl_close($ch);
//return $result;
// curl_close($ch); // Close connection
$response = json_decode($result);
print_r($response);
?>
This code works fine for native android app, but titanium developed app, when I send push notification from above script, device gets notification but with "NULL" payload
.
I want to know why? What's wrong with my PHP script.
UPDATE
This is my code for receiving notifications
// These events monitor incoming push notifications
CloudPush.addEventListener('callback', function (evt) {
//Ti.API.info('This is the payload data i got'+JSON.parse(evt.payload));
Ti.API.log("This is the GCM response "+JSON.stringify(evt));
alert(evt.payload);
//var payload = JSON.parse(evt.payload);
//Ti.API.info('This is the message data i got'+payload.message);
});
CloudPush.addEventListener('trayClickLaunchedApp', function (evt) {
Ti.API.info('Tray Click Launched App (app was not running)');
});
CloudPush.addEventListener('trayClickFocusedApp', function (evt) {
Ti.API.info('Tray Click Focused App (app was already running)');
});
and this is the answer
[INFO] : APSCloudPush: receivePayload: null
[INFO] : APSCloudPush: background: true
[INFO] : APSCloudPush: queuePayload: null
[INFO] : APSCloudPush: showTrayNotification
[ERROR] : APSCloudPush: Payload is null!
+3
source to share
1 answer
The above code is absolutely correct. I think the problem is just the keyword payload. Just replace the word "message" with "payload" on the server side script as shown below.
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "payload" => $message,
)
);
Because in Titanium, the ti.cloudePush
module internally looks for the word payload
+2
source to share