分享

ThinkPHP框架整合极光推送DEMO

 小马哥技术屋 2016-11-29
极光推送(JPush)是独立的第三方云推送平台,致力于为全球移动应用开发者提供专业、高效的移动消息推送服务。

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

    1、将极光推送DEMO文件(文件夹名称为Jpush)放入到你的公共文件夹(Common)中,按照极光开发文档在极光后台建立好自己的应用,获取相应的app_key、master_secret,在文件中将会用到这两个值;

        wKioL1eub_7ziHNYAABFZLTAOKE110.png

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

  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、最后便是在控制器中进行调用即可;

  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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多