城市飞艇推送:响应:从服务器获得否定响应:0

Posted

技术标签:

【中文标题】城市飞艇推送:响应:从服务器获得否定响应:0【英文标题】:Urban Airship push: Response: Got negative response from server: 0 【发布时间】:2014-01-18 03:57:37 【问题描述】:

我正在尝试从我的服务器向我的 android 应用发送推送通知。但它抛出错误Payload: "audience":"all","notification":"android":"alert":"php script test ","device_types":["android"] Response: Got negative response from server: 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 脚本。城市飞艇服务器推送通知正常。

感谢任何形式的帮助。

【问题讨论】:

【参考方案1】:
<?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 是您要推送的通知。在这两个变量中正确赋值。它会工作..

【讨论】:

【参考方案2】:

根据 HTTP 标准,您应该在“Content-Type:”和“application/json”之间包含一个空格。可能 Google 服务器没有正确解释您的内容类型,使其成为不正确的请求(甚至可能由于服务器端的 Accept: 值而被拒绝)。

更正您的内容类型标题并重试

【讨论】:

以上是关于城市飞艇推送:响应:从服务器获得否定响应:0的主要内容,如果未能解决你的问题,请参考以下文章

如何在android的城市飞艇中注册api?

iPhone 推送通知 - 错误响应问题

城市飞艇证书

从城市飞艇用户信息中提取警报

解析从 android onMessageReceived 上的 fcm 响应获得的 json

使用没有错误消息的私人频道时无法从推送者那里获得响应