php实现极光消息推送

首先在极光注册一个账号,然后创建一个应用,需要上传应用名称和图标,然后下一把上传包名

 上传成功后在设置里拿到appkey和masterSecret  上传后可以下一个demo做测试

 

 RegId  就是registration_id   高级功能里可以设置别名和标签

极光推送的文档https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#audience

function send() {
        $m_type = 'http';//推送附加字段的类型
        $m_txt = 'http://www.groex.cn/';//推送附加字段的类型对应的内容(可不填) 可能是url,可能是一段文字。
        $m_time = 86400;//离线保留时间
        $platform = 'all';
        $receive= ["registration_id" => [ "123fffe2232132134" ]];  // ["alias" => [ "123" ]];  可以是别名 也可以registration_id  别名需要在demo里设置 
        $content = '好玩';
        $return_data = jiguang_send($receive, $content, $platform, $m_type, $m_txt, $m_time);
        return $return_data;
    }


/**
     * Notes:
     * User: Dong
     * Date: 2019/8/5
     * Time: 14:23
     * @param $receive 推送目标 别名、标签、注册 ID、分群、广播
     * @param $content 推送内容
     * @param $platform 推送平台  Android, iOS, Windows Phone 三个平台的推送 all 或者 { "platform" : ["android", "ios"] }
     * @param $m_type extras JSON 格式的可选参数 key/value 形式
     * @param $m_txt  extras JSON 格式的可选参数 key/value 形式 可以传文字或者url让app端识别 跳转到特定页面
     * @param $m_time int    离线保留时间
     * @return mixed
     */
    function jiguang_send($receive, $content, $platform, $m_type, $m_txt, $m_time)
    {
        $appkey = C('JIGUANG_APPKEY'); //AppKey
        $secret = C('JIGUANG_SECRET'); //Secret

        $postUrl = "https://api.jpush.cn/v3/push";

        $base64           = base64_encode("$appkey:$secret");
        $header           = array("Authorization:Basic $base64", "Content-Type:application/json"); //调用验证
        $data             = array();
        $data['platform'] = $platform;     //目标用户终端手机的平台类型android,ios,winphone
        $data['audience'] = $receive;      //目标用户

        $data['notification'] = array(
            //统一的模式--标准模式
            "alert"   => $content,
            //安卓自定义
            "android" => array(
                "alert"  => $content,
                "title" => "庭好的家装",
                "builder_id" => 1,
                "extras" => array("type" => $m_type, "txt" => $m_txt)
            ),
            //ios的自定义
            "ios"     => array(
                "alert"  => $content,
                "badge"  => "1",
                "sound"  => "default",
                "extras" => array("type" => $m_type, "txt" => $m_txt)
            )
        );

        //苹果自定义---为了弹出值方便调测
        $data['message'] = array(
            "msg_content" => $content,
            "extras"      => array("type" => $m_type, "txt" => $m_txt)
        );

        //附加选项
        $data['options'] = array(
            "sendno"          => time(),   //推送序号
            "time_to_live"    => $m_time,  //离线消息保留时长(秒)
            "apns_production" => false,    //布尔类型   指定 APNS 通知发送环境:0开发环境,1生产环境。或者传递false和true
        );
        $param           = json_encode($data);
        $curlPost = $param;

        $ch = curl_init();                                              //初始化curl
        curl_setopt($ch, CURLOPT_URL, $postUrl);                 //抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);                    //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);            //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);                      //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);           // 增加 HTTP Header(头)里的字段
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        // 终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $return_data = curl_exec($ch);                                 //运行curl
        curl_close($ch);
        $return_data = json_decode($return_data,true);          //如果发送失败会有error 其中有错误码和错误信息
        if(empty($return_data['error'])){
            return true;
        }else{
            return false;
        }
    }

发送成功 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值