极光推送(JPush)是独立的第三方云推送平台,致力于为全球移动应用开发者提供专业、高效的移动消息推送服务。php
本篇博文讲述如何在将极光推送DEMO整合到ThinkPHP框架中,我使用的是极光推送PHP_DEMO_V3.4.3版本:json
一、将极光推送DEMO文件(文件夹名称为Jpush)放入到你的公共文件夹(Common)中,按照极光开发文档在极光后台创建好本身的应用,获取相应的app_key、master_secret,在文件中将会用到这两个值;数组

二、如上,在公共文件夹(Common)下创建function.php文件;app
- /**
- * 将数据先转换成json,而后转成array
- */
- function json_array($result){
- $result_json = json_encode($result);
- return json_decode($result_json,true);
- }
-
- /**
- * 向全部设备推送消息
- * @param string $message 须要推送的消息
- */
- function sendNotifyAll($message){
- require_once "JPush\JPush.php";
- $app_key = 'your app_key'; //填入你的app_key
- $master_secret = 'your master_secret'; //填入你的master_secret
- $client = new \JPush($app_key,$master_secret);
- $result = $client->push()->setPlatform('all')->addAllAudience()->setNotificationAlert($message)->send();
- return json_array($result);
- }
-
-
- /**
- * 向特定设备推送消息
- * @param array $regid 特定设备的设备标识
- * @param string $message 须要推送的消息
- */
- function sendNotifySpecial($regid,$message){
- require_once "JPush\JPush.php";
- $app_key = 'your app_key'; //填入你的app_key
- $master_secret = 'your master_secret'; //填入你的master_secret
- $client = new \JPush($app_key,$master_secret);
- $result = $client->push()->setPlatform('all')->addRegistrationId($regid)->setNotificationAlert($message)->send();
- return json_array($result);
- }
-
- /**
- * 向指定设备推送自定义消息
- * @param string $message 发送消息内容
- * @param array $regid 特定设备的id
- * @param int $did 状态值1
- * @param int $mid 状态值2
- */
-
- function sendSpecialMsg($regid,$message,$did,$mid){
- require_once "JPush\JPush.php";
- $app_key = 'your app_key'; //填入你的app_key
- $master_secret = 'your master_secret'; //填入你的master_secret
- $client = new \JPush($app_key,$master_secret);
- $result = $client->push()->setPlatform('all')->addRegistrationId($regid)
- ->addAndroidNotification($message,'',1,array('did'=>$did,'mid'=>$mid))
- ->addIosNotification($message,'','+1',true,'',array('did'=>$did,'mid'=>$mid))->send();
-
- return json_array($result);
- }
-
- /**
- * 获得各种统计数据
- * @param array $msgIds 推送消息返回的msg_id列表
- */
- function reportNotify($msgIds){
- require_once "JPush\JPush.php";
- $app_key = 'your app_key'; //填入你的app_key
- $master_secret = 'your master_secret'; //填入你的master_secret
- $client = new \JPush($app_key,$master_secret);
- $response = $client->report()->getReceived($msgIds);
- return json_array($response);
- }
在文件中写入各类集成函数,以方便在系统应用控制器中进行调用。框架
三、最后即是在控制器中进行调用便可;函数
- //向特定用户进行推送—单播
- //$regid能够是一个单个regid组成的字符串,也能够是多个regid组成的数组
- //$data['content']是你所须要推送的内容
- $result_s = sendNotifySpecial($regid, $data['content']);
-
- //想全部用户进行推送—广播
- $result_a = sendNotifyAll($data['content']);
-
- //获取统计用户是否获取推送消息的信息(或者有多少用户收到了推送消息)
- //$msgids是你推送消息的消息id
- $result_r = reportNotify($msgIds);