1
votes

I am attempting to do push notifications to my iOS app using FCM but am having problems with the sound. I followed this post and implemented the 'sound' key-value pair accordingly but firebase throw this error. Could anyone advice pls?

Error sending message: { Error: Invalid JSON payload received. Unknown name "sound" at 'message.notification': Cannot find field.

My JSON implementation on cloud functions as follows:

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

        var message = {
            notification: {
                body: 'New message(s)',
                sound: 'default'
            },

              token: fcmToken
            };

        return admin.messaging().send(message)
          .then((response) => {
            return console.log('Successfully sent message:', response);
          })
          .catch((error) => {
            return console.log('Error sending message:', error);
          });
    })
1
Hello, any help here? - Koh
Basing entirely from the docs, the sound parameter is specifically for Android only, which is probably the reason why you are encountering the invalid parameter error. But this is for when using Cloud Functions, have you tested using the REST API instead? See if it works. - AL.
@AL. not true. Referring to this doc and this doc I suppose it is also valid for iOS - Koh
The first documentation you linked clearly shows sound under the Android notification object. I'm not saying it's not entirely possible. I'm just saying that it's not possible for Cloud Functions, hence I suggested test sending a payload via REST API. - AL.
@AL. able to show some code? I need to use a database trigger to send this pushNotification. Not too sure how do I implement a REST API through this... - Koh

1 Answers

2
votes

I fear I'm late at this point but this is using HTTP v1 API. Just as all the android specific settings should go in an android object, iOS specific settings should go in an APNs object.

var message = {
        notification: {
            title: "Title Notification"
            body: "New message(s)"
        },
        token: fcmToken,
        apns: {
            aps:{
                sound:"default"
            }
        } 
    };