0

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)
user1872384
  • 6,886
  • 11
  • 61
  • 103
  • 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 Jun 16 '18 at 04:17
  • 1
    The token is invalid. Same call stack as [this question](https://stackoverflow.com/q/50420582/4815718). See the comments there. – Bob Snyder Jun 16 '18 at 05:01
  • 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 working – user1872384 Jun 19 '18 at 14:09
  • I don't know much about iOS and can't suggest anything--sorry. – Bob Snyder Jun 19 '18 at 14:17
  • 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 Jun 19 '18 at 14:19
  • Just noticed i've left out the notification: {} in the payload. =D – user1872384 Jun 19 '18 at 16:43

1 Answers1

0

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)
Rohit Maurya
  • 730
  • 1
  • 9
  • 22