firebase推送

google firebase推送(官方文档

  1. 准备工作
    获取firebase后台的推送密钥,单个用户推送需要手客户端收集用户的token,全局推送还要拿到客户端配置的主题topic
    在这里插入图片描述
  2. 拼凑参数,android和ios有所不同
    public function getPush($firebase_resultactivity) {
            $res = array();
            if($this->IosOrAndroid == 1){
            	//ios
                $res['title'] = $this->title?:'';//消息标题
                $res['body'] = $this->message?:'';//消息内容
                $res['sound'] = "XXXX";//系统提示音
            }else{
            	//android
                $res['data']['title'] = $this->title?:'';//消息标题
                $res['data']['message'] = $this->message?:'';//消息内容
                $res['data']['resultActivity'] = $firebase_resultactivity;//客户端提供,配置之后可以在未打开游戏的情况下,点击消息启动游戏
            }
            return $res;
        }
    
  3. 发送请求
    private function sendPushNotification($fields) {
        $firebase_key = $fields['firebase_key'];//firebase后台的key
        unset($fields['firebase_key']);
        $url = 'https://fcm.googleapis.com/fcm/send';//请求链接
        //请求头
        $headers = array(
            'Authorization: key=' . $firebase_key,
            'Content-Type: application/json'
        );
    
        // Open connection
        $ch = curl_init();
    
        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $url);
    
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
        // Disabling SSL Certificate support temporarly
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
    
        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('Curl failed: ' . curl_error($ch));
        }
    
        // Close connection
        curl_close($ch);
        return $result;
    
  4. 查看返回结果,以下结果即为推送成功,查看客户端是否会收到消息

    {“multicast_id”:8083597852777327546,“success”:1,“failure”:0,“canonical_ids”:0,“results”:[{“message_id”:“0:1624346763678416%6bd3ddb8f9fd7ecd”}]}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值