编程语言
首页 > 编程语言> > php – Urban Airship push:响应:来自服务器的否定响应:0

php – Urban Airship push:响应:来自服务器的否定响应:0

作者:互联网

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

感谢提前获得任何帮助.

解决方法:

<?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,android,urbanairship-com
来源: https://codeday.me/bug/20190629/1323774.html