極光推送(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);