ThinkPHP框架整合極光推送DEMO


  極光推送(JPush)是獨立的第三方雲推送平台,致力於為全球移動應用開發者提供專業、高效的移動消息推送服務。

    本篇博文講述如何在將極光推送DEMO整合到ThinkPHP框架中,我使用的是極光推送PHP_DEMO_V3.4.3版本:

    1、將極光推送DEMO文件(文件夾名稱為Jpush)放入到你的公共文件夾(Common)中,按照極光開發文檔在極光后台建立好自己的應用,獲取相應的app_key、master_secret,在文件中將會用到這兩個值;

        wKioL1eub_7ziHNYAABFZLTAOKE110.png

    2、如上,在公共文件夾(Common)下建立function.php文件;

[php]  view plain  copy
 
  1.     /**     
  2.      * 將數據先轉換成json,然后轉成array 
  3.      */  
  4.     function json_array($result){  
  5.        $result_json = json_encode($result);  
  6.        return json_decode($result_json,true);  
  7.     }  
  8.       
  9.     /** 
  10.      * 向所有設備推送消息 
  11.      * @param string $message 需要推送的消息 
  12.      */  
  13.     function sendNotifyAll($message){  
  14.        require_once "JPush\JPush.php";  
  15.        $app_key = 'your app_key';                //填入你的app_key  
  16.        $master_secret = 'your master_secret';    //填入你的master_secret  
  17.        $client = new \JPush($app_key,$master_secret);  
  18.        $result = $client->push()->setPlatform('all')->addAllAudience()->setNotificationAlert($message)->send();  
  19.        return json_array($result);  
  20.     }  
  21.       
  22.       
  23.     /** 
  24.      * 向特定設備推送消息 
  25.      * @param array $regid 特定設備的設備標識 
  26.      * @param string $message 需要推送的消息 
  27.      */  
  28.     function sendNotifySpecial($regid,$message){  
  29.        require_once "JPush\JPush.php";  
  30.        $app_key = 'your app_key';                //填入你的app_key  
  31.        $master_secret = 'your master_secret';    //填入你的master_secret  
  32.        $client = new \JPush($app_key,$master_secret);  
  33.        $result = $client->push()->setPlatform('all')->addRegistrationId($regid)->setNotificationAlert($message)->send();  
  34.        return json_array($result);  
  35.     }  
  36.       
  37.     /** 
  38.      * 向指定設備推送自定義消息 
  39.      * @param string $message 發送消息內容 
  40.      * @param array $regid 特定設備的id 
  41.      * @param int $did 狀態值1 
  42.      * @param int $mid 狀態值2 
  43.      */  
  44.       
  45.     function sendSpecialMsg($regid,$message,$did,$mid){  
  46.        require_once "JPush\JPush.php";  
  47.        $app_key = 'your app_key';                //填入你的app_key  
  48.        $master_secret = 'your master_secret';    //填入你的master_secret  
  49.        $client = new \JPush($app_key,$master_secret);  
  50.        $result = $client->push()->setPlatform('all')->addRegistrationId($regid)  
  51.           ->addAndroidNotification($message,'',1,array('did'=>$did,'mid'=>$mid))  
  52.           ->addIosNotification($message,'','+1',true,'',array('did'=>$did,'mid'=>$mid))->send();  
  53.       
  54.        return json_array($result);  
  55.     }  
  56.       
  57.     /** 
  58.      * 得到各類統計數據 
  59.      * @param array $msgIds 推送消息返回的msg_id列表 
  60.      */  
  61.     function reportNotify($msgIds){  
  62.        require_once "JPush\JPush.php";  
  63.        $app_key = 'your app_key';                //填入你的app_key  
  64.        $master_secret = 'your master_secret';    //填入你的master_secret  
  65.        $client = new \JPush($app_key,$master_secret);  
  66.        $response = $client->report()->getReceived($msgIds);  
  67.        return json_array($response);  
  68.     }  

    在文件中寫入各種集成函數,以方便在系統應用控制器中進行調用。

 

    3、最后便是在控制器中進行調用即可;

[php]  view plain  copy
 
  1.     //向特定用戶進行推送—單播  
  2.     //$regid可以是一個單個regid組成的字符串,也可以是多個regid組成的數組  
  3.     //$data['content']是你所需要推送的內容  
  4.     $result_s = sendNotifySpecial($regid, $data['content']);  
  5.       
  6.     //想所有用戶進行推送—廣播  
  7.     $result_a = sendNotifyAll($data['content']);  
  8.       
  9.     //獲取統計用戶是否獲取推送消息的信息(或者有多少用戶收到了推送消息)  
  10.     //$msgids是你推送消息的消息id  
  11.     $result_r = reportNotify($msgIds);  

 

版權聲明:轉載時請標注http://blog.csdn.net/zhihua_w


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM