Koh*_*Koh 4 node.js firebase firebase-cloud-messaging
我正在尝试使用适用于 iOS 的 Cloud Functions 和 FCM 实现推送通知,但我始终抛出此错误:
2018-05-21T13:04:00.087ZI sendPushNotifications:发送消息时出错:{错误:请求包含无效参数。在 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: { code: 'messaging/invalid-argument', message: '请求包含无效参数。' }, 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);
});
})
})
Run Code Online (Sandbox Code Playgroud)
令人沮丧的是,当我删除整个"apns"节点时,代码实际上可以工作,即我可以收到推送通知。我想这意味着我的设置都已正确完成。一旦我包含了"apns",它就开始抛出上述错误。我还参考了这三篇文章,this,this和this,并确保我仔细遵循了代码和说明。由于某些原因我无法让它工作。
我还尝试删除该"notification"节点,因为文档确实提到仅在针对所有平台时使用通用密钥。由于我现在只针对 iOS,我想我应该删除该"notification"密钥。但它也会再次抛出同样的错误。
好吧,所以这是一个菜鸟错误。如果我仅针对 iOS,则不应使用通用密钥,这是正确的。除此之外,徽章应该是 aInt而不是String。
这段代码有效:
var message = {
"token": fcmToken,
"apns": {
"headers": {
"apns-priority": "5"
},
"payload": {
"aps": {
"alert": {
"body": "New message"
},
"badge": 1,
"sound": "default"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
希望它可以帮助任何面临同样问题的人。
| 归档时间: |
|
| 查看次数: |
3632 次 |
| 最近记录: |