【发布时间】:2018-10-31 04:02:36
【问题描述】:
我正在尝试使用 Cloud Functions 和 FCM for iOS 实现 pushNotifications,但我一直抛出此错误:
2018-05-21T13:04:00.087Z 我发送推送通知:发送错误 消息:{ 错误:请求包含无效参数。 在 FirebaseMessagingError.Error (本机) 在 FirebaseMessagingError.FirebaseError [作为构造函数] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28) 在 FirebaseMessagingError.PrefixedFirebaseError [作为构造函数] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28) 在新的 FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:241:16) 在 Function.FirebaseMessagingError.fromServerError (/user_code/node_modules/firebase-admin/lib/utils/error.js:271:16) 在 /user_code/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:149:50 在 process._tickDomainCallback (internal/process/next_tick.js:135:7) errorInfo: { 代码: '消息/无效参数', 消息:“请求包含无效参数。” }, codePrefix: '消息' }
我在云函数中的实现如下:
exports.sendPushNotifications = functions.database.ref('/conversations/{userUid}/').onWrite((snap, context) => {
const userUid = context.params.userUid
console.log("Triggered user ", userUid)
return admin.database().ref('/fcmToken/' + userUid).once('value', snapshot => {
const values = snapshot.val()
const fcmToken = values.fcmToken
var message = {
"token": fcmToken,
"notification": {
"body": "New message"
},
"apns": {
"headers": {
"apns-priority": "5"
},
"payload": {
"aps": {
"alert": {
"body": "New message"
},
"badge": "1",
"sound": "default"
}
}
}
};
return admin.messaging().send(message)
.then((response) => {
return console.log('Successfully sent message:', response);
})
.catch((error) => {
return console.log('Error sending message:', error);
});
})
})
令人沮丧的是,当我删除整个"apns" 节点时,代码实际上可以工作,即我可以接收推送通知。我想这意味着我的设置都已正确完成。一旦我包含了"apns",它就会开始抛出上述错误。我还参考了这三个帖子,this、this 和 this,并确保我已仔细遵循代码和说明。由于某些原因,我无法让它工作。
我还尝试删除 "notification" 节点,因为文档确实提到在针对所有平台时只使用公共键。由于我现在只针对 iOS,我想我应该删除 "notification" 键。但同样它也会引发同样的错误。
【问题讨论】:
标签: node.js firebase firebase-cloud-messaging