FCM推送成功但安卓设备未收到通知。

8
当我尝试通过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"
    }]
}

Android端出现了一个错误。请问Android开发人员是否已经添加了正确的代码以接收通知? - KuLdip PaTel
请勿在 Stack Overflow 上公开您的 FCM api_key - KuLdip PaTel
1
到目前为止有任何解决方案吗? - Amit Kumar Pawar
https://stackoverflow.com/a/51495856/894671 - Guntis Treulands
3个回答

8

将“data”更名为“notification”

如果“notification”对象不存在,则设备将不会在抽屉中显示通知。

应用程序解释“data”。

设备解释“notification”。

您可以分别使用两者,但如果您想显示通知,则需要“notification”对象。

参考文献


5

完美的答案@Chetan,使用"data"通知不会显示,但使用"notification"会显示。

因此,您的数组应该是:

$fields = array (
    'registration_ids' => array (
            $device_id
    ),
    'notification' => array (
        "title" => "test from server",
        "body" => "test lorem ipsum"
    )
);

2
我通过卸载并重新安装应用程序来解决了这个问题。

这个答案并没有解决当前的问题。重新安装应用程序可能无法解决当前的问题,最可预测的情况是应用程序包含一些失败的流程,并且无法接收来自Firebase的通知。 - Franco Gil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接