سؤال

I am trying to send push notification to my Android app from my server. But it is throwing error Payload: {"audience":"all","notification":{"android":{"alert":"PHP script test "}},"device_types":["android"]} Response: Got negative response from server: 0.

Below 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 am trying to run this php script from my browser. Push notification from urban airship server is working properly.

Thanks advance for any kind of help.

هل كانت مفيدة؟

المحلول

<?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 array type. It is list of aliases. $message is notification which you want to push. Assign value properly in this two variable. It will work..

نصائح أخرى

According to the HTTP standards, you should include a space between "Content-Type:" and "application/json". Probably the Google server is not interpreting your content-type correctly making it an incorrect request ( It could even be it is rejected due to the Accept: values on the server side ).

Correct your content-type header and try again

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top