Google FCM 服务器:200 但未向 phone 发送通知
Google FCM Server: 200 but no notification sent to phone
我正在通过 Postman 将 FCM 推送通知发送到我已经安装了该应用程序的 phone。我已启用推送通知,应用程序在后台运行,并且我有一个有效(已确认)的 Firebase 推送令牌。
我正在使用 headers(授权:key=APP_SERVER_KEY、Content-Type:application/json)和我的 body 看起来像:
{ "notification": {
"title": "Portugal vs. Denmark",
"text": "5 to 1"
},
"to": MY_PUSH_TOKEN
}
我收到以下 200 OK 响应body:
{
"multicast_id": MULTICAST_ID,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [{
"message_id": "0:MESSAGE_ID"
}]
}
一切正常,但我在 phone 上没有收到通知?我该如何解决这个问题,有人知道我错过了什么吗?
找到解决方案!
但在我提供解决方案之前,这里有一些关于可能遇到类似问题的其他人的更多背景信息。我能够 POST 并向 Android 设备发送通知,并且能够通过 Firebase 控制台向 Android 和 iOS 设备发送推送,所以唯一不起作用的是通过 POST HTTP 请求将推送发送到特定的 iOS 设备(相当令人费解)。
我在这里找到了这个讨论:https://github.com/firebase/quickstart-ios/issues/21。基本上对于 iOS,我在请求正文中缺少两个其他参数。我需要将 content_available 设置为 true,并将优先级设置为高。
所以看起来像这样:
{
"notification": {
"title": "Portugal vs. Denmark",
"body": "5 to 1"
},
"content_available": true,
"priority": "high",
"to": MY_PUSH_TOKEN
}
我相信 github 讨论谈到了这些添加的请求正文参数如何允许 FCM 通过 APNS 为 iOS 推送。
感谢您的回答并解释您的背景。
在我的上下文中,它无论如何都不起作用。
所以我添加了 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)
}
使用 php 脚本向 iOS 设备发送推送通知的替代工作解决方案。
只需根据评论更改字段数组中的键 'to' 和 header 数组中的授权的值
//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 );
?>
我正在通过 Postman 将 FCM 推送通知发送到我已经安装了该应用程序的 phone。我已启用推送通知,应用程序在后台运行,并且我有一个有效(已确认)的 Firebase 推送令牌。
我正在使用 headers(授权:key=APP_SERVER_KEY、Content-Type:application/json)和我的 body 看起来像:
{ "notification": {
"title": "Portugal vs. Denmark",
"text": "5 to 1"
},
"to": MY_PUSH_TOKEN
}
我收到以下 200 OK 响应body:
{
"multicast_id": MULTICAST_ID,
"success": 1,
"failure": 0,
"canonical_ids": 0,
"results": [{
"message_id": "0:MESSAGE_ID"
}]
}
一切正常,但我在 phone 上没有收到通知?我该如何解决这个问题,有人知道我错过了什么吗?
找到解决方案!
但在我提供解决方案之前,这里有一些关于可能遇到类似问题的其他人的更多背景信息。我能够 POST 并向 Android 设备发送通知,并且能够通过 Firebase 控制台向 Android 和 iOS 设备发送推送,所以唯一不起作用的是通过 POST HTTP 请求将推送发送到特定的 iOS 设备(相当令人费解)。
我在这里找到了这个讨论:https://github.com/firebase/quickstart-ios/issues/21。基本上对于 iOS,我在请求正文中缺少两个其他参数。我需要将 content_available 设置为 true,并将优先级设置为高。
所以看起来像这样:
{
"notification": {
"title": "Portugal vs. Denmark",
"body": "5 to 1"
},
"content_available": true,
"priority": "high",
"to": MY_PUSH_TOKEN
}
我相信 github 讨论谈到了这些添加的请求正文参数如何允许 FCM 通过 APNS 为 iOS 推送。
感谢您的回答并解释您的背景。 在我的上下文中,它无论如何都不起作用。
所以我添加了 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)
}
使用 php 脚本向 iOS 设备发送推送通知的替代工作解决方案。 只需根据评论更改字段数组中的键 'to' 和 header 数组中的授权的值
//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 );
?>