简体   繁体   English

使用PHP向使用钛(跨平台)开发的Android应用程序发送推送通知

[英]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. 要从服务器向Android应用程序发送推送通知,我使用以下脚本。

<?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 . 此代码适用于原生Android应用程序,但当我发送带有上述脚本的推送通知时,应用程序开发的应用程序接收通知,但具有“NULL” payload

I want to know, why? 我想知道,为什么? What is wrong with my PHP Script. 我的PHP脚本出了什么问题。

UPDATE 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. 只需在服务器端脚本中将“message”替换为“payload”,如下所示。

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

Because in Titanium ti.cloudePush module internally search for word payload 因为在Titanium ti.cloudePush模块内部搜索单词有效负载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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