gpt4 book ai didi

php - Urban Airship 推送 : Response: Got negative response from server: 0

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:01:55 25 4
gpt4 key购买 nike

我正在尝试从我的服务器向我的 Android 应用程序发送推送通知。但它抛出错误 Payload: {"audience":"all","notification":{"android":{"alert":"PHP script test "}},"device_types":["android"] } 响应:从服务器得到否定响应:0.

下面是源码

<?php
define('APPKEY','**************Mw'); // Your App Key
define('PUSHSECRET','**********Low'); // Your Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/push/');

$contents = array();
$contents['alert'] = "PHP script test";
$notification = array();
$notification['android'] = $contents;
$platform = array();
array_push($platform, "android");

$push = array("audience"=>"all", "notification"=>$notification, "device_types"=>$platform);

$json = json_encode($push);
echo "Payload: " . $json . "\n"; //show the payload

$session = curl_init(PUSHURL);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json', 'Accept: application/vnd.urbanairship+json; version=3;'));
$content = curl_exec($session);
echo "Response: " . $content . "\n";

// Check if any error occured
$response = curl_getinfo($session);
if($response['http_code'] != 202) {
echo "Got negative response from server: " . $response['http_code'] . "\n";
} else {

echo "Wow, it worked!\n";
}

curl_close($session);

?>

我正在尝试从我的浏览器运行这个 php 脚本。来自 Urban Airship 服务器的推送通知工作正常。

提前感谢您提供的任何帮助。

最佳答案

<?php
// DEVELOPMENT PUSH DETAILS
define('APPKEY','XXXXXXXXXXXXXXXXXXX');
define('PUSHSECRET', 'XXXXXXXXXXXXXXXXXXX'); // Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/push/');
/*
// PRODUCTION PUSH DETAILS
define('APPKEY','XXXXXXXXXXXXXXXXXXX');
define('PUSHSECRET', 'XXXXXXXXXXXXXXXXXXX'); // Master Secret
define('PUSHURL', 'https://go.urbanairship.com/api/push/');
*/

$push = array();

$push['aliases'] = $aliases; // Using alias that is set from the javascript after the device has registered to urban airship
$push['aps'] = array("badge"=>"+1", "alert" => $message); // for iphone
$push['android'] = array("alert"=>$message); // for android

$json = json_encode($push);

echo "Payload: " . $json . "\n"; //show the payload

$session = curl_init(PUSHURL);

curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($session, CURLOPT_USERPWD, APPKEY . ':' . PUSHSECRET);
curl_setopt($session, CURLOPT_POST, True);
curl_setopt($session, CURLOPT_POSTFIELDS, $json);
curl_setopt($session, CURLOPT_HEADER, False);
curl_setopt($session, CURLOPT_RETURNTRANSFER, True);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

$content = curl_exec($session); // $content has all the data that came back from urban airship..check its contents to see if successful or not.


// Check if any error occured
$response = curl_getinfo($session);

if($response['http_code'] != 200) {
$status = $response['http_code'];
echo "Got negative response from server: " . $response['http_code'] . "\n";
} else {
$status = 'ok';
}

curl_close($session);

?>

这里,$aliases 是数组类型。它是别名列表。 $message 是您要推送的通知。在这两个变量中正确分配值。它会工作..

关于php - Urban Airship 推送 : Response: Got negative response from server: 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20853539/

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