FCM 成功但 android 设备未收到通知

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

    }]

}


回首忆惘然
浏览 298回答 3
3回答

桃花长相依

完美答案@Chetan,不会显示“数据”通知,但会显示“通知”。所以你的数组应该是:$fields = array (&nbsp; &nbsp; 'registration_ids' => array (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $device_id&nbsp; &nbsp; ),&nbsp; &nbsp; 'notification' => array (&nbsp; &nbsp; &nbsp; &nbsp; "title" => "test from server",&nbsp; &nbsp; &nbsp; &nbsp; "body" => "test lorem ipsum"&nbsp; &nbsp; ));

冉冉说

将“数据”重命名为“通知”如果“通知”对象不存在,设备将不会在抽屉中显示通知。“数据”由应用程序解释“通知”由设备解释..您可以单独使用两者,但如果要显示通知,则需要“通知”对象。

精慕HU

我已经通过删除应用程序并重新安装来解决问题。
打开App,查看更多内容
随时随地看视频慕课网APP