我正在尝试从我的服务器向我的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 DETAILSdefine('APPKEY','XXXXXXXXXXXXXXXXXXX'); define('PUSHSECRET', 'XXXXXXXXXXXXXXXXXXX'); // Master Secretdefine('PUSHURL', 'https://go.urbanairship.com/API/push/'); /*// PRODUCTION PUSH DETAILSdefine('APPKEY','XXXXXXXXXXXXXXXXXXX'); define('PUSHSECRET', 'XXXXXXXXXXXXXXXXXXX'); // Master Secretdefine('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 push:响应:来自服务器的否定响应:0全部内容,希望文章能够帮你解决php – Urban Airship push:响应:来自服务器的否定响应:0所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)