FCM Notification in iOS doesn't play sound when received
Asked Answered
C

6

38

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
Clomp answered 5/9, 2016 at 17:34 Comment(1)
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 .Novation
D
75

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"
}
Dressler answered 6/9, 2016 at 10:13 Comment(7)
I am having the same structure on server but still on some devices sound file is not playing when app is in background.Romansh
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 .Novation
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?Respondence
Thank you, it worked for me. Would you please provide the source of your answer? Thank you.Dygert
@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.Vaginal
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.")) firebase.google.com/docs/cloud-messaging/ios/topic-messagingCl
Attention, this is only true if you use legacy api, and not http v1 api. see this page: firebase.google.com/docs/cloud-messaging/migrate-v1Bathsheba
P
47

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)

Pastern answered 26/6, 2018 at 17:14 Comment(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.Convict
B
11
    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

Bleb answered 9/7, 2018 at 13:38 Comment(0)
G
5

If you build an application using Flutter, There are many possibilities for sound not to play

  1. You may be using an older version of flutter_local_notifications Please Update it to the latest.
  2. Missing sound in apns payload

If you want to play a custom sound, you can add your sound file inside the resource in iOS and pass the file name conditionally inside the payload.

  1. If you pass the default then the default ringtone will play.
  2. If you pass file name which is inside iOS resources folder then that sound ring.

Code Example:

const message = {
    token: to,
    notification: {
      title,
      body,
      // sound: "ringtone.wav",
    },
    apns: {
      headers: {
        "apns-priority": "10",
      },
      payload: {
        aps: {
          sound:
            body != "You have one Booking Request"
              ? "default"
              : "ringtone.wav",
        },
      },
    },
    android: {
      priority: "high",
      notification: {
        channelId:
          body != "You have one Booking Request" ? "normal" : "Sound",
        sound: "ringtone.mp3",
      },
    },
    data: {
      click_action,
    },
  };

Replace body != "You have one Booking Request" ? "default" : "ringtone.wav" with your requirment

Grovergroves answered 31/3 at 14:18 Comment(0)
O
4

Using firebase-admin package for node js

    const message = {
        notification: {
          title: "This is title",
          body: "This is body",
        },
        apns: {
          headers: {
            "apns-priority": "10",
          },
          payload: {
            aps: {
              sound: "default",
            },
          },
        },
        android: {
          notification: {
            sound: "default",
          },
        },
        token: 'fcm-token'
    }


getMessaging()
    .send(message)
    .then((response) => {
      res.json({
        msg: "Sent successfully",
      });
      console.log(response);
    })
    .catch((err) => console.log(err));
Oleary answered 7/9, 2023 at 2:48 Comment(0)
A
1

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

Ackler answered 18/7, 2021 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.