1

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);
          });
    })
Koh
  • 2,687
  • 1
  • 22
  • 62
  • Hello, any help here? – Koh May 21 '18 at 05:21
  • Basing entirely from the [docs](https://firebase.google.com/docs/cloud-messaging/admin/send-messages#android_specific_fields), 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. May 21 '18 at 07:48
  • @AL. not true. Referring to this [doc](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages) and this [doc](https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW1) I suppose it is also valid for iOS – Koh May 21 '18 at 13:22
  • 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. May 21 '18 at 13:58
  • @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 May 21 '18 at 14:01

1 Answers1

2

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"
            }
        } 
    };
Miguel Beltran
  • 2,132
  • 1
  • 23
  • 36
DEVCNN
  • 614
  • 4
  • 13