0
votes

I'm trying to deploy cloud function to create push notification (chat messaging) on firebase (Firestore). But when i'm trying to do this - i'm always getting HTTP Error: 400, The request has errors. Looks like path of collections is good.

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
                })
                
                
            })
            
        })
        
    }
    
    
})

also i would ask about line "clickAction: "ChatActivity" it only for android? how can i do same to ios? Thx a lot!

1
Please edit the question to show the full error message in the context of your deployment. If you're deploying with the Firebase CLI, show its output.Doug Stevenson
its all CLI answer... "HTTP Error: 400, The request has errors"Pasha Fateev
Make sure you are using the latest version of the CLI. If the CLI is not giving you an actionable error message, you should contact Firebase support for assistance. support.google.com/firebase/contact/supportDoug Stevenson
@DougStevenson i have also "Error: Functions did not deploy properly."Pasha Fateev

1 Answers

0
votes

Try to delete the function from firebase console and redeploy

or

try changing Internet Service Provider and deploy