Phonegap Push Notification + Additional PHP JAVASCRIPT information

Asked

Viewed 63 times

1

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 reports 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, getting it like this:

$data = ['title'=> 'Teste' ,'message' => 'Recebendo uma mensagem push!', 'action'=> 'acao'];

And in javascript I can only get the title and the message the action. 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);


});

Data.message and data.title Alert return the right value but, data.action returns undifined

  • How are you sending this in PHP to Javascript? JSON? and how are you receiving value in Javascript?

  • @Sergio updated the question with the javascript function.

  • What gives alert(Object.keys(data));?

  • @Sergio , I managed to solve I have to use the following way to get the value Alert(data.additionalData.action).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.