获取key的方法:
登录谷歌开发者后台 https://console.firebase.google.com/?hl=zh-cn
function firebaseNotice($title,$body){$token_arr=['token1','token2']; //用户的firebasetoken列表$notify_msg = ["notification" => ["title" => $title,"body" => $body],"data" => [], //由开发者自己定义内容"registration_ids" => $token_arr,"direct_boot_ok"=>true];$data = json_encode($notify_msg);$notify_server_key = 'AAAAk5ct8mI:A*******1LLtP6jWH'; //替换为自己的firebase开发者后台获取的key$url = "https://fcm.googleapis.com/fcm/send";$headers = ["Authorization:key=" . $notify_server_key,"Content-Type:application/json"];try {$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);// 在尝试连接时等待的秒数curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 2);// 最大执行时间curl_setopt($ch, CURLOPT_TIMEOUT, 3);$result = curl_exec($ch);//var_dump($result);$respon = json_decode($result, true);} catch (\Exception $e) {$respon = [];}if (isset($respon['results'])) {if (isset($respon['results']['error'])) {return false;} else {return true;}}return true;}
调用方法:
$title='系统通知';
$body='通知内容通知内容';
firebaseNotice($title,$body);