3
votes

I am attempting to implement pushNotifications using Cloud Functions and FCM for iOS but I am consistently thrown this error:

2018-05-21T13:04:00.087Z I sendPushNotifications: 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' }

My implementation in cloud functions are as follows:

exports.sendPushNotifications = functions.database.ref('/conversations/{userUid}/').onWrite((snap, context) => {
    const userUid = context.params.userUid

    console.log("Triggered user ", userUid)

    return admin.database().ref('/fcmToken/' + userUid).once('value', snapshot => {
        const values = snapshot.val()
        const fcmToken = values.fcmToken

        var message = {
            "token": fcmToken,

            "notification": {
                "body": "New message"
            },
            "apns": {
                "headers": {
                    "apns-priority": "5"
                },
                "payload": {
                    "aps": {
                        "alert": {
                            "body": "New message"
                        },
                        "badge": "1",
                        "sound": "default"
                    }
                }
            }
        };

        return admin.messaging().send(message)
          .then((response) => {
            return console.log('Successfully sent message:', response);
          })
          .catch((error) => {
            return console.log('Error sending message:', error);
          });
    })
})

The frustrating thing is that when I remove the whole "apns" node the code actually works, ie I can receive the push notifications. I suppose this means my setup are all done properly. Once I included the "apns", it starts throwing the above error. I also reference these three posts, this, this and this, and made sure that I have carefully followed the code and instructions. For some reasons I cannot get it to work.

I also attempted to remove the "notification" node as the docs did mention that only use common keys when targetting all platforms. Since I am targetting only iOS for now, I suppose I should remove the "notification" key. But again it also throws the same error.

1

1 Answers

4
votes

Ok, so it was a rookie mistake. It is correct that common keys should not be used if I am targetting iOS only. In addition to that, the badge should be an Int and not String.

This code worked:

var message = {
    "token": fcmToken,

    "apns": {
        "headers": {
            "apns-priority": "5"
        },
        "payload": {
            "aps": {
                "alert": {
                    "body": "New message"
                },
                "badge": 1,
                "sound": "default"
            }
        }
    }
}

Hope it helps anyone out there facing the same problem.