google firebase推送(官方文档)
- 准备工作
获取firebase后台的推送密钥,单个用户推送需要手客户端收集用户的token,全局推送还要拿到客户端配置的主题topic
- 拼凑参数,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; }
- 发送请求
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;
- 查看返回结果,以下结果即为推送成功,查看客户端是否会收到消息
{“multicast_id”:8083597852777327546,“success”:1,“failure”:0,“canonical_ids”:0,“results”:[{“message_id”:“0:1624346763678416%6bd3ddb8f9fd7ecd”}]}