Urban Airship push:响应:来自服务器的负面响应:0

时间:2013-12-31 07:23:24

标签: php android urbanairship.com

我正在尝试从我的服务器向我的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脚本。城市飞艇服务器的推送通知工作正常。

感谢提前获得任何帮助。

2 个答案:

答案 0 :(得分:3)

<?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是您要推送的通知。在这两个变量中正确分配值。它会工作..

答案 1 :(得分:0)

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

更正您的内容类型标题,然后重试