原文转载自 PHP嘉友站
http://www.phpjyz.com/article-36-1.html
最近在使用PHP給Android提供数据接口,其他接口功能基本上调试完毕,最后剩下推送的部分。前端开发人员选择了极光推送,于是小编跑到其官网上面看了一下PHP的SDK,按照其上面的配置搭建好环境。按照官方在Github提供的示例:
[代码]php代码:
2 | $client = new JPushClient( $app_key , $master_secret ); |
3 | $payload = new PushClient(); |
4 | $notification = new Notification(); |
5 | $notification ->alert = "alert message" ; |
6 | $result1 = $client ->sendPush( $payload ); |
这段代码运行有问题,百思不得其解。后来下载官方的demo,终于搞定,写下基于极光推送的Android推送代码类:
[代码]php代码:
08 | include_once './JpushSdk/JPushClient.php' ; |
09 | include_once './JpushSdk/model/Audience.php' ; |
10 | include_once './JpushSdk/model/Message.php' ; |
11 | include_once './JpushSdk/model/notification/Notification.php' ; |
12 | include_once './JpushSdk/model/notification/IOSNotification.php' ; |
13 | include_once './JpushSdk/model/notification/AndroidNotification.php' ; |
14 | include_once './JpushSdk/model/notification/WinphoneNotification.php' ; |
15 | include_once './JpushSdk/model/Options.php' ; |
16 | include_once './JpushSdk/model/Platform.php' ; |
17 | include_once './JpushSdk/model/PushPayload.php' ; |
22 | private $master_secret = 'xxxxxxxxxxxxxxxxx' ; |
23 | private $app_key = 'xxxxxxxxxxxxxxxxxxxxxx' ; |
29 | public function push( $msg ){ |
30 | $client = new JPushClient( $this ->app_key, $this ->master_secret); |
32 | $payload1 = new PushPayload(); |
33 | $notification = new Notification(); |
34 | $notification ->alert = $msg ; |
35 | $android = new AndroidNotification(); |
36 | $android ->alert = $msg ; |
38 | $notification ->android = $android ; |
39 | $payload1 ->notification = $notification ; |
41 | $result1 = $client ->sendPush( $payload1 ); |
43 | $rA = json_decode( $result1 , true); |
44 | return $rA [ 'sendno' ] == 0; |
备注:使用前先下载极光推送PHP的Sdk