在Firebase中部署云功能时请求错误。 HTTP错误:400

时间:2020-10-13 21:13:52

标签: node.js firebase google-cloud-firestore google-cloud-functions firebase-cloud-messaging

我正在尝试部署云功能以在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做同样的事情? 非常感谢!

1 个答案:

答案 0 :(得分:0)

尝试从Firebase控制台删除该功能并重新部署

尝试更改Internet服务提供商并进行部署