安排urbanairship推送通知

时间:2014-07-14 06:53:40

标签: php urbanairship.com

阅读文档后,我尝试编写以下来源:

$contents = array();
 //$contents['badge'] = "+1";
 $contents['alert'] = "Touchdown!";
 //$contents['sound'] = "cat.caf";
 $notification = array();
 $notification= $contents;

 $audience['tag'] = "49ers";

 $scheduled['scheduled_time'] = "2013-04-01T18:45:30";

 $push['push'] = array("audience"=> $audience, "notification"=>$notification, "device_types"=>"all");
 $response = array();
 array_push($response, array("name" => "My schedule", "schedule"=> $scheduled));
 array_push($response, $push);


 $json = json_encode($response);
 //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";
print_r($content);
 // Check if any error occured
 $response = curl_getinfo($session);
 if($response['http_code'] != 202) {  
 $error = "Got negative response from server: " . $response['http_code'] . "\n";
 } else {

     $success = "Message has been sent successfully";
 }

我收到错误= 0。 有人可以帮帮我吗?

感谢。

1 个答案:

答案 0 :(得分:1)

我已经在@dperconti的帮助下找到了问题(非常感谢你)。

我正在发布一个解决方案'如何安排推送urbanairship',以便其他人可能会发现它有用。

代码是:

 define('APPKEY','xxxx'); // Your App Key
 define('PUSHSECRET', 'xxxx'); // Your Master Secret
 define('PUSHURL', 'https://go.urbanairship.com/api/schedules/');

 $contents = array();
 //$contents['badge'] = "+1";
 $contents['alert'] = "Touchdown!";
 //$contents['sound'] = "cat.caf";
 $notification = array();
 $notification= $contents;

 $audience = "all";

 $scheduled['scheduled_time'] = "2016-04-01T18:45:30";

//   $push['push'] = array("audience"=> $audience, "notification"=>$notification, "device_types"=>"all");
 $response = array();
 array_push($response, array("name" => "My schedule", "schedule"=> $scheduled, "push" => array("audience"=> $audience, "notification"=>$notification, "device_types"=>"all")));
//   array_push($response, $push);


 $json = json_encode($response);
 echo "Payload: " . $json . "<br><br><br>"; //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";
print_r($content);
 // Check if any error occured
 $response = curl_getinfo($session);
 if($response['http_code'] != 202) {
     $error = "Got negative response from server: " . $response['http_code'] . "\n";
 } else {

     $success = "Message has been sent successfully";
 }

 curl_close($session);