资料连接:
//代码:
//准备配置 $url = 'https://fcm.googleapis.com/fcm/send'; $api_key = '秘钥'; //准备消息内容 $data = [ 'message' => '123132', 'noticeType' => '2', //推送类型 // 'image'=>'',// 'payload'=>'',// 'timestamp'=> date('Y-m-d H:i:s',time() - TimeLag()), // 'title'=>'梦',// 'is_background'=>'', ]; $fields = array( //群发使用registration_ids[为数组] 单个用户使用to 'to' => '要推送用户的id',// 'registration_ids' => xxx, 'data' => $data,// 'time_to_live' => xxx, ); $headers = array( 'Authorization: key=' . $api_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); curl_setopt($ch, CURLOPT_TIMEOUT, 5); //请求超时时间这里设置为5s // 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); // Close connection curl_close($ch);var_dump(json_decode($result,true));die;//收到一下内容为成功
array(5) {
["multicast_id"]=>
int(*****************)(已隐藏)
["success"]=>
int(1)
["failure"]=>
int(0)
["canonical_ids"]=>
int(0)
["results"]=>
array(1) {
[0]=>
array(1) {
["message_id"]=>
string(35) "**********************"(已隐藏)
}
}
}
来源:https://www.cnblogs.com/dream-meng/p/13029265.html