$messag-6ren">
gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:19:58 24 4
gpt4 key购买 nike

要从服务器向 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);
?>

此代码适用于 native 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!

最佳答案

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

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

因为在 Titanium ti.cloudePush 模块内部搜索单词 payload

关于php - 使用 PHP 向钛(跨平台)开发的 android 应用程序发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30182543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com