简体   繁体   中英

Sending push-notification to android app developed in titanium (cross-platform) using PHP

To send push-notification from server to 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 the app developed in titanium when i send push-notification with above script, device receives notification but with "NULL" payload .

I want to know, why? What is wrong with my PHP Script.

UPDATE

This is my code to receive notification

// 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 response

[INFO] :   APSCloudPush: receivePayload: null
[INFO] :   APSCloudPush: background: true
[INFO] :   APSCloudPush: queuePayload: null
[INFO] :   APSCloudPush: showTrayNotification
[ERROR] :  APSCloudPush: Payload is null!

The code above given is absolutely correct. I guess the problem is only the keyword payload. Just replace the word "message" to "payload" at server side script as below.

    $fields = array(
    'registration_ids' => $registrationIDs,
    'data' => array( "payload"  => $message,
                     )
     );

Because in Titanium ti.cloudePush module internally search for word payload

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM