【问题标题】:Sending push-notification to android app developed in titanium (cross-platform) using PHP使用 PHP 向钛(跨平台)开发的 android 应用程序发送推送通知
【发布时间】:2015-07-22 20:02:57
【问题描述】:

要将推送通知从服务器发送到 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);
?>

此代码适用于本机 android 应用程序,但是当我使用上述脚本发送推送通知时,在钛开发的应用程序,设备收到通知但“NULL”payload

我想知道,为什么?我的 PHP 脚本出了什么问题。

更新

这是我接收通知的代码

// 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)');
 });

这是回应

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

【问题讨论】:

  • 脚本没有错,显示你的接收推送消息的钛代码
  • @MurtazaKhursheedHussain 请查看更新后的问题。
  • 您使用的是哪个版本的 CloudPush?
  • SDK 版本 3.5.1.GA
  • 可能是插件坏了什么的。尝试使用早期版本,如 3.2.1

标签: php android push-notification titanium


【解决方案1】:

上面给出的代码是绝对正确的。我想问题只是关键字有效载荷。只需在服务器端脚本中将“消息”一词替换为“有效负载”,如下所示。

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

因为在 Titanium ti.cloudePush 模块内部搜索单词有效载荷

【讨论】:

  • 感谢有关“消息”->“有效负载”的提示,这也解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 2019-06-10
  • 1970-01-01
  • 2015-11-23
  • 2019-01-12
相关资源
最近更新 更多