当我尝试通过 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" }]}
3 回答
桃花长相依
TA贡献1860条经验 获得超8个赞
完美答案@Chetan,不会显示“数据”通知,但会显示“通知”。
所以你的数组应该是:
$fields = array (
'registration_ids' => array (
$device_id
),
'notification' => array (
"title" => "test from server",
"body" => "test lorem ipsum"
)
);
冉冉说
TA贡献1877条经验 获得超1个赞
将“数据”重命名为“通知”
如果“通知”对象不存在,设备将不会在抽屉中显示通知。
“数据”由应用程序解释
“通知”由设备解释..
您可以单独使用两者,但如果要显示通知,则需要“通知”对象。
- 3 回答
- 0 关注
- 296 浏览
添加回答
举报
0/150
提交
取消