使用PHP给Android移动客户端推送消息

原文转载自 PHP嘉友站 


http://www.phpjyz.com/article-36-1.html


最近在使用PHPAndroid提供数据接口,其他接口功能基本上调试完毕,最后剩下推送的部分。前端开发人员选择了极光推送,于是小编跑到其官网上面看了一下PHPSDK,按照其上面的配置搭建好环境。按照官方在Github提供的示例:

[代码]php代码:

1 //发送广播通知
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代码:

01 <?php
02 /**
03  * AndroidPush
04  * 针对极光推送的Android推送消息的类
05  * @author phpjyz.com
06  */
07  
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';
18  
19 class AndroidPush{
20      
21     //这个两个成员是极光推送平台来提供的
22     private $master_secret 'xxxxxxxxxxxxxxxxx';
23     private $app_key 'xxxxxxxxxxxxxxxxxxxxxx';
24      
25     /**
26      * 推送消息
27      * @param unknown $msg
28      */
29     public function push( $msg ){
30         $client new JPushClient($this->app_key, $this->master_secret);
31         //发送广播通知
32         $payload1 new PushPayload();
33         $notification new Notification();
34         $notification->alert = $msg;
35         $android new AndroidNotification();
36         $android->alert = $msg;
37          
38         $notification->android = $android;
39         $payload1->notification = $notification;
40         $payload1->toJSON();
41         $result1 $client->sendPush($payload1);
42         //$result1 = '{"sendno":"0","msg_id":"2037636937"}';
43         $rA = json_decode($result1, true);
44         return $rA['sendno'] == 0;
45     }
46 }
47 ?>      


备注:使用前先下载极光推送PHP的Sdk

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值