Hello, I'm trying to use the following function to pass additional information to my app using phonegap and push notification.
function sendGoogleCloudMessage( $data, $ids )
{
// Insert real GCM API key from Google APIs Console
// https://code.google.com/apis/console/
$apiKey = 'AIzaSyBaCn_OE7l_-KdzgfIdtF68uAcCXo0A9VE';
// Define URL to GCM endpoint
$url = 'https://gcm-http.googleapis.com/gcm/send';
// Set GCM post variables (device IDs and push payload)
$post = array(
'registration_ids' => $ids,
'data' => $data,
);
// Set CURL request headers (authentication and type)
$headers = array(
'Authorization: key=' . $apiKey,
'Content-Type: application/json'
);
// Initialize curl handle
$ch = curl_init();
// Set URL to GCM endpoint
curl_setopt( $ch, CURLOPT_URL, $url );
// Set request method to POST
curl_setopt( $ch, CURLOPT_POST, true );
// Set our custom headers
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
// Get the response back as string instead of printing it
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Set JSON post data
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
// Actually send the push
$result = curl_exec( $ch );
// Error handling
if ( curl_errno( $ch ) )
{
echo 'GCM error: ' . curl_error( $ch );
}
// Close curl handle
curl_close( $ch );
// Debug GCM response
echo $result;
}
The information I pass through here:
$data = ['title'=> 'Teste' ,'message' => 'Recebendo uma mensagem push!'];
// The recipient registration tokens for this notification
// http://developer.android.com/google/gcm/
$ids = [
'dE4kDutF1NY:APA91bE4vo4SzFVbg1pefg39pcRqYo0PMqOz6wvV1ve_chC-o4nNgb1VfV2yDiayiy6zgoWgqPcdebKknMHx45Cf9hFxrSeJw5vHGA-1l8z8L3Wovm0Rad2qEPavtzQKy90iDOdpP-tQ'
];
I tried to add more information in $ data, staying as follows:
$data = ['title'=> 'Teste' ,'message' => 'Recebendo uma mensagem push!', 'action'=> 'acao'];
And in the javascript I can only get the title and the message the action does not. Would anyone know what I'm doing wrong?
The javascript function is as follows:
push.on('notification', function(data) {
alert(data.message);
alert(data.title);
alert(data.action);
});
The alert of data.message and data.title return the right value but, the data.action returns undifined