【发布时间】:2021-01-28 06:22:02
【问题描述】:
我正在尝试部署云功能以在 Firebase(Firestore)上创建推送通知(聊天消息)。 但是当我尝试这样做时 - 我总是收到 HTTP 错误:400,请求有错误。 看起来收藏路径不错。
exports.notifyNewMessage = functions.firestore
.document('/chat/{toUserId}/chatRoom/{fromUserId}/chatItems')
.onCreate((docSnapshot, context) => {
const message = docSnapshot.data();
const recipientId = context.params.toUserId; // получатель сообщения
const senderId = context.params.fromUserId; // отправитель сообщения
const senderName = message['username'];
if (recipientId === senderId) {
} else {
return admin.forestore().doc('tokens/' + recipientId).get().then(userDoc => {
const tokens = userDoc.get('tokens')
const notificationBody = (message['type'] === "TEXT") ? message['textMessage'] : "New message with Image"
const payload = {
notification: {
title: senderName + " sent you a message",
body: notificationBody,
clickAction: "ChatActivity" // возможно, это только для андроида
},
data: {
USER_NAME: senderName,
USER_ID: message['senderId']
}
}
return admin.messaging().sendToDevice(tokens, payload).then( response => {
const stillRegisteredTokens = tokens
response.results.forEach((result, index) => {
const error = result.error
if (error) {
const failedRegistrationToken = tokens[index]
console.error('failed token', failedRegistrationToken, error)
if (error.code === 'messaging/invalid-registration-token' || error.code == 'messaging/registration-token-not-registred') {
const failedIndex = stillRegisteredTokens.indexOf(failedRegistrationToken)
if (failedIndex > -1) {
stillRegisteredTokens.splice(failedIndex, 1)
}
}
}
})
return admin.firestore().doc("tokens" + recipientId).update({
tokens: stillRegisteredTokens
})
})
})
}
})
我也想问一下“clickAction:”ChatActivity“它只适用于android?我怎么能对ios做同样的事情? 非常感谢!
【问题讨论】:
-
请编辑问题以在您的部署上下文中显示完整的错误消息。如果您使用 Firebase CLI 进行部署,请显示其输出。
-
它的所有 CLI 答案...“HTTP 错误:400,请求有错误”
-
确保您使用的是最新版本的 CLI。如果 CLI 没有为您提供可操作的错误消息,您应该联系 Firebase 支持以寻求帮助。 support.google.com/firebase/contact/support
-
@DougStevenson 我也有“错误:功能没有正确部署。”
标签: node.js firebase google-cloud-firestore google-cloud-functions firebase-cloud-messaging