简体   繁体   English

适用于Android的PHP中的城市飞艇推送通知

[英]Urban airship push notification for Android in PHP

I am making an Android location based app. 我正在制作一个基于Android位置的应用。 For the location alert I want to send push notification in Android. 对于位置警报,我想在Android中发送推送通知。 But I am new to Urban airship. 但是我是城市飞艇的新手。 Can anyone tell me how can I implement it in PHP web API. 谁能告诉我如何在PHP Web API中实现它。 It will be thankful to me 我会很感激

This is a good article that should get you started. 这是一篇很好的文章 ,应该让您入门。 It has both Android and iOS code as well. 它还具有Android和iOS代码。

Here is the code specifically for Android: 这是专门针对Android的代码:

<?php
 define('APPKEY','XXXXXXXXXXXXXXX'); // Your App Key
 define('PUSHSECRET', 'XXXXXXXXXXXXXXX'); // 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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM