极光推送

2016年10月20日 16:24:01 星期四

发给指定用户的流程:

1. 用户每次登录, 都会生成一个新的 registration ID 存入数据库, 与uid对应

2. 发送时取出该registration ID, 当作参数传递给接口发送

 1 require 'path_to/JPush/autoload.php';
 2 
 3 use JPushClient as JPush;
 4 
 5 class JCPush
 6 {
 7     public $client = FALSE;
 8     public function __construct()
 9     {
10         $appkey = C('JPush_Appkey');
11         $master_secret = C('JPush_Master_Secret');
12         $this->client = new JPush($appkey, $master_secret);
13     }
14 
15     public function pushMsg($uid, $msg, $platform='all')
16     {
17         $jpushId = M('table')->where(['uid' => $uid])->getField('registration_id'); //可以写入缓存, 不要循环查库
18         $response = $this->client->push()
19             ->setPlatform($platform)
20             ->addRegistrationId($jpushId)
21             ->setNotificationAlert($msg)
22             ->send();
23         return $response;
24 
25 //        Array
26 //        (
27 //            [body] => Array
28 //                          (
29 //                              [sendno] => 0
30 //                            [msg_id] => 3318558882
31 //                        )
32 //
33 //            [http_code] => 200
34 //            [headers] => Array
35 //                     (
36 //                         [rateLimitLimit] => 600
37 //                        [rateLimitRemaining] => 599
38 //                        [rateLimitReset] => 60
39 //                    )
40 //
41 //        )
42     }
43 }
原文地址:https://www.cnblogs.com/iLoveMyD/p/5981385.html