極光推送(JPush)是獨立的第三方雲推送平台,致力於為全球移動應用開發者提供專業、高效的移動消息推送服務。
本篇博文講述如何在將極光推送DEMO整合到ThinkPHP框架中,我使用的是極光推送PHP_DEMO_V3.4.3版本:
1、將極光推送DEMO文件(文件夾名稱為Jpush)放入到你的公共文件夾(Common)中,按照極光開發文檔在極光后台建立好自己的應用,獲取相應的app_key、master_secret,在文件中將會用到這兩個值;
2、如上,在公共文件夾(Common)下建立function.php文件;
- /**
- * 將數據先轉換成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);
- }
在文件中寫入各種集成函數,以方便在系統應用控制器中進行調用。
3、最后便是在控制器中進行調用即可;
- //向特定用戶進行推送—單播
- //$regid可以是一個單個regid組成的字符串,也可以是多個regid組成的數組
- //$data['content']是你所需要推送的內容
- $result_s = sendNotifySpecial($regid, $data['content']);
- //想所有用戶進行推送—廣播
- $result_a = sendNotifyAll($data['content']);
- //獲取統計用戶是否獲取推送消息的信息(或者有多少用戶收到了推送消息)
- //$msgids是你推送消息的消息id
- $result_r = reportNotify($msgIds);
版權聲明:轉載時請標注http://blog.csdn.net/zhihua_w