0
votes

I'm trying too use node.js and firebase admin to send a push notification to an iOS device. However, I'm hitting this error:

Error sending message: { Error: Request contains an invalid argument. at FirebaseMessagingError.Error (native) at FirebaseMessagingError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28) at FirebaseMessagingError.PrefixedFirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:85:28) at new FirebaseMessagingError (/user_code/node_modules/firebase-admin/lib/utils/error.js:241:16) at Function.FirebaseMessagingError.fromServerError (/user_code/node_modules/firebase-admin/lib/utils/error.js:271:16) at /user_code/node_modules/firebase-admin/lib/messaging/messaging-api-request.js:149:50 at process._tickDomainCallback (internal/process/next_tick.js:135:7) errorInfo: { code: 'messaging/invalid-argument', message: 'Request contains an invalid argument.' }, codePrefix: 'messaging' }

// See documentation on defining a message payload.

var message = {
    data: {
        score: '850'
    },
    token: '1B269EFDFB4370C037F584XXXX5AA08AD206FB1FC29398AB786F2694F8D50XXX'
};

// Send a message to the device corresponding to the provided
// registration token.
res.send(admin.messaging().send(message)
1
I have the feeling there a lot of code you're not showing here. What's res? Why are there more opening parens than closing parens on the last line?Doug Stevenson
The token is invalid. Same call stack as this question. See the comments there.Bob Snyder
Thanks @BobSnyder it's really cause by invalid token. Change it to "FIRMessaging.messaging.FCMToken" and I'm able to receive notification when the app is in the foreground. How to receive the notification when the app is terminated? I've tried to set "content_available: true" in the message and set the priority: "high" and enabled the background mode in capabilities however it's still not workinguser1872384
I don't know much about iOS and can't suggest anything--sorry.Bob Snyder
It's ok thanks @BobSnyder, appreciate your help. I'll try to figure it out, since I'm able to receive the notification using the firebase console when the app is terminated.user1872384

1 Answers

0
votes

As you havn't provided enough information,so i'm giving you overview to send notification to device from function node.js you have to use something like this:

var token = "1B269EFDFB4370C037F584XXXX5AA08AD206FB1FC29398AB786F2694F8D50XXX"
    var payload = {
            data:{
                username: request.userName,
            }
        };
        admin.messaging().sendToDevice(token, payload)