加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:52:06 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试从我的服务器向我的 Android应用程序发送推送通知.但它正在抛出错误Payload:{“audience”:“all”,“notification”:{“android”:{“alert”:“PHP script test”}},“device_types”:[“android”]}响应:得到了服务器的否定回复: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,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','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,APPKEY . ':' . PUSHSECRET); 
curl_setopt($session,True); 
curl_setopt($session,$json); 
curl_setopt($session,False); 
curl_setopt($session,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是您要推送的通知.在这两个变量中正确分配值.它会工作..

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读