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文件;

    /**    
     * 将数据先转换成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);


  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值