谷歌 FCM 服务器:200 但手机未收到通知

5
我正在通过Postman向已安装应用程序的手机发送FCM推送通知。我已启用推送通知,该应用程序在后台运行,并且我拥有有效的(已确认)Firebase推送令牌。我正在使用以下标题(Authorization: key=APP_SERVER_KEY, Content-Type: application/json)POST到https://fcm.googleapis.com/fcm/send,我的主体如下:
{ "notification": {
"title": "Portugal vs. Denmark",
"text": "5 to 1"
},
 "to": MY_PUSH_TOKEN
}

我收到了以下带有正文的200 OK响应:

{
 "multicast_id": MULTICAST_ID,
 "success": 1,
 "failure": 0,
 "canonical_ids": 0,
 "results": [{
  "message_id": "0:MESSAGE_ID"
 }]
}

一切看起来都很正常,但是我没有在手机上收到通知?我该如何进行故障排除,有人知道我漏掉了什么吗?


什么设备?安卓还是iOS? - AL.
我已经尝试了您的消息,并且能够在客户端(在此情况下为Android)接收到该消息。我猜这是客户端问题,因此添加您的接收代码将有所帮助。另外,请在请求中使用“body”而不是“text”,因为“text”不是通知负载的文档化字段。 - Arthur Thompson
AL,我正在针对iOS进行开发。 @ArthurThompson,我已将其更改为文本。我还尝试在Android设备上运行它,它可以工作,但是在iOS上仍然没有成功。您所提到的接收代码是什么?我正在尝试通过FCM服务器通过Postman HTTP请求发送。 - Ke Cheng
接收代码将位于您的AppDelegate中,您可以在其中注册远程通知等。 - Arthur Thompson
3个回答

12

找到了解决方案!

但在提供解决方案之前,以下是更多背景信息,以帮助其他可能遇到类似问题的人。我能够通过POST方式向Android设备发送通知,也能够通过Firebase控制台向Android和iOS设备发送推送通知,所以唯一无法正常工作的是通过POST HTTP请求将推送发送到特定的iOS设备(非常令人困惑)。

我在这里找到了讨论:https://github.com/firebase/quickstart-ios/issues/21。基本上对于iOS设备,我的请求体中缺少两个参数,需要将content_available设置为true,并将优先级设置为high。

因此,它看起来像这样:

{
    "notification": {
        "title": "Portugal vs. Denmark",
        "body": "5 to 1"
    },
    "content_available": true,
    "priority": "high",
    "to": MY_PUSH_TOKEN
}

我相信 Github 的讨论中提到了这些额外的请求体参数如何允许 FCM 通过 APNS 推送 iOS。


2
感谢您的回答并解释您的背景。在我的情况下,它无论如何都不起作用。
所以我添加了content_available,并且还将“text”替换为“body”。
如果仍在与此斗争,请看这里是我如何处理的。
干杯!
import request from 'request'

const KEYS = require('../hardcodekeys')

export function send (title, message, ids) {

  //POST: https://fcm.googleapis.com/fcm/send
  //HEADER: Content-Type: application/json
  //HEADER: Authorization: key=AIzaSy*******************
  let push = {
    "notification" : {
      "body" : "great match!",
      "title" : "Portugal vs. Denmark",
      "icon" : "myicon"
    },
    content_available: true,
    to: ids
    priority: 'high'
  }

  let options = {
    method: 'POST',
    url: 'https://fcm.googleapis.com/fcm/send',
    headers: {
      'Content-Type': 'application/json',
      'Authorization' : `key=${KEYS.FCM_KEY}`
    },
    body: JSON.stringify(push)
  }

  function callback(error, response, body) {
    console.log(response.statusCode)
    console.log(body)
    console.log(error)
    if (!error && response.statusCode == 200) {
      var info = JSON.parse(body)
      console.log(info)
    }
  }

  request(options, callback)
}

0

Alternative working solution to send push notification to iOS device using php script. Just change value for keys 'to' from fields array and Authorization from header array as per comment

//To Execute this this from Mac Terminal type php thisFilename.php


$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array (
        'to' => 'fq5fI_wcXNY:APA91bGpR-qCYW01wNJ8pnp1ftfgR3DHqPk3ViXDqTYrq-p7MUhry9cPcpXEl7z4GFHGUPcTduww656Rks8cOpSZR-FjrseDX4S-eGdzGaBEqI46KWF8ZJmJpegbf3tzVZwILmnf64aU',//Replace with FIRInstanceID.instanceID().token() this you can get in Appdelegate, note this is NOT token received in didRegisterForRemoteNotificationsWithDeviceToken
        'notification' => array (
                "body" => "message",
                "title" => "Title",
                "icon" => "myicon"
        )
);
$fields = json_encode ( $fields );
$headers = array (
        'Authorization: key=' . "AIdfdfzaSyC_0F8sqVqOgdg3Es4trWFcrNcrLpBjG6w06w",//This is Server Key, you can get it from Firebase console ->  App Setting ->  Cloud Messaging Tab - Legacy server key
        'Content-Type: application/json'
);

$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_POSTFIELDS, $fields );

$result = curl_exec ( $ch );
curl_close ( $ch );


?>


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