PHP – urban aircraft push: response: negative response from server: 0

I'm trying to send a push notification from my server to my Android application. But it's throwing an error. Payload: {"audience": "all", "notification": {"Android": {"alert": "PHP script test"}}, "device_types": ["Android"]} response: got a negative reply from the server: 0

The following is the source code

<?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);

?>

I tried to run this PHP script from my browser. The push notification from the city airship server works normally

Thanks for any help in advance

resolvent:

<?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);

?>

Here, $aliases is an array type. It is a list of aliases. $message is the notification you want to push. Assign values correctly in these two variables. It will work

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>