当我尝试通过 cURL 请求发送推送通知时,来自服务器的响应表明我已成功,但设备上未收到消息。我已经尝试过使用多播和单个收件人有效负载。
这是我的PHP代码:
<?php
//API URL of FCM
$url = 'https://fcm.googleapis.com/fcm/send';
/*api_key available in:
Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key*/ $api_key = 'API_KEY';
$device_id = 'eE5U0IQEyTo:APA91bGplai6Bf5ko1hlW5y0oLb0WIa5JytpcuZ7B9lbIay8PNfPv2i1HMUqg1hDtPQqvhy4KLIZgyEh0BHHkfJtdX7E0Ftm-OaN23VahOoWAzjNP2QK8Se7PCibhooVG71jMPmzTHqd';
$fields = array (
'registration_ids' => array (
$device_id
),
'data' => array (
"title" => "test from server",
"body" => "test lorem ipsum"
)
);
//header includes Content type and api key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$api_key
);
$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_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE)
{
die('FCM Send Error: ' . curl_error($ch));
}
else
{
curl_close($ch);
print_r($result);
return $result;
}
?>
这是我在运行此代码时得到的响应:
{
"multicast_id": 1338828245860499776,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [{
"message_id": "0:1578484075615332%52ec0605f9fd7ecd"
}]
}
桃花长相依
冉冉说
精慕HU