30

I am using Firebase push notifications in my iOS Application. Although I am able to send the notification by sending below payload, it doesn't play a sound when received.

{
    "to": "myToken",
    "notification": {
        "body": "test",
        "title": "test"
    },
    "priority": "high"
    "sound": "default"
}

If I send the test message from console, it works well and plays notification sound.
Note:

  1. My Authorization code is correct
  2. I am sending http request to https://fcm.googleapis.com/fcm/send
  3. I have tested it on IPhone 4 , IPhone 6 and IPhone 6S, All recieve notifications without sound
AL.
  • 36,815
  • 10
  • 142
  • 281
Jr Pro
  • 301
  • 1
  • 3
  • 8
  • i am getting notifications from fcm . now , i want to make functionality such as when i will get notification from fcm , app should play a sound file without even touching notification message . do you know how to do that ? didRecieveRemoteNotification will only get executed when user tap on notification . i want to play a sound file like alarm tune when i get the notification . please guide me through it . – Moxarth Jul 06 '17 at 10:21

4 Answers4

71

your JSON "sound" : "default" should be inside the "notification" key not in the root of the JSON. This JSON should work.

{
    "to": "myToken",
    "notification": {
         "body": "test",
         "title": "test",
         "sound": "default"
    },
    "priority": "high"
}
kaitosenpai
  • 973
  • 1
  • 7
  • 12
  • I am having the same structure on server but still on some devices sound file is not playing when app is in background. – VikasGoyal Mar 09 '17 at 05:35
  • i am getting notifications from fcm . now , i want to make functionality such as when i will get notification from fcm , app should play a sound file without even touching notification message . do you know how to do that ? didRecieveRemoteNotification will only get executed when user tap on notification . i want to play a sound file like alarm tune when i get the notification . please guide me through it . – Moxarth Jul 06 '17 at 10:22
  • 1
    where should this json file exist ? because i'm testing the notification via ui firebase.com , so how can i send this payload with sound? – Joe Sleiman Jul 17 '17 at 12:23
  • Thank you, it worked for me. Would you please provide the source of your answer? Thank you. – Cyril Sep 19 '18 at 00:19
  • 1
    @DuncanJones, actually, your answer didn't work for me with iOS 12.1 but putting `sound:'default'` in the non-device-specifc `notification` object ultimately worked for me. – Works for a Living May 14 '19 at 07:03
  • but it takes only two arguments (title and body) if you do it without json: `Message.builder() .setNotification(new Notification( "$GOOG up 1.43% on the day", "$GOOG gained 11.80 points to close at 835.67, up 1.43% on the day."))` https://firebase.google.com/docs/cloud-messaging/ios/topic-messaging – user924 Jun 10 '19 at 19:03
  • Attention, this is only true if you use legacy api, and not http v1 api. see this page: https://firebase.google.com/docs/cloud-messaging/migrate-v1 – Manu Eidenberger Oct 28 '19 at 16:12
38

When using the FCM admin SDK, you have to specify sounds separately for Android and Apple devices:

let message = {
    notification: {
        'body': 'This is the message the user sees',
    },
    data: {
        'param1': 'specify some extra data here',
    },
    // Apple specific settings
    apns: {
        headers: {
            'apns-priority': '10',
        },
        payload: {
            aps: {
                sound: 'default',
            }
        },
    },
    android: {
      priority: 'high',
      notification: {
          sound: 'default',
      }
    },
    token: 'target FCM token goes here',
};

(Note: I've only tested the Apple settings thus far)

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • 1
    Good call - I haven't been able to find any info on getting notification sounds to work in IOS anywhere, didn't realise it was part of the APNS object! Cheers for this. – JVG Jun 27 '18 at 01:01
10
    payload = {
        notification:{
            title: 'SOLO has been changed by an administrator',
            body: 'Administrator changed your SOLO schedule',
        },
        android: {
        },
        apns: {
            headers:{
                "apns-collapse-id": "solo_changed_administrator",
                "content-available": "1",
                "apns-priority": "10",
            },
            payload:{
                aps:{
                    sound: 'default',
                    badge: 12213123223
                }
            }
        },
        data:{
            type: 'type'
        }
    }

https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages?authuser=0#ApnsConfig

Yaroslav Malyk
  • 409
  • 5
  • 15
0

I had the same problem. When notifications come through FCM to iOS not working sounds or vibrations. Here I followed this link: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW1 And finally went successful. I created my apple notification as an alert in my payload and it works for me. Here is my JSON below to get some idea about my solution.

"apns": {
    "payload": {
        "aps" : {
            "alert" : {
                "body": "Notification body",
                "title": "Notification title"
            },
            "badge" : 2,
            "sound" : "default"
        }
    }
}

Note: Please put this "apns" key in the relevant place of your message request. I was using REST call for requesting message. Please refer the following link to get a good idea of how to send a notification message with platform-specific delivery options. Link: https://firebase.google.com/docs/cloud-messaging/concept-options#example-notification-message-with-platform-specific-delivery-options