'Custom sound push notification does not work (Flutter)

{
  "to": "XXXX",
  "notification": {
    "title": "ASAP Alert",
    "body": "Please open your app"
  },
  "data": {
    "screen": "/Nexpage1",
    "sound": "alarm",
    "click_action": "FLUTTER_NOTIFICATION_CLICK"
  }
}

Above is my payload for the push notification. I have insert the alarm.mp3 file inside the raw folder, however it still does not give me the alarm sound, i have try for alarm.mp3 also, is there anything wrong with the json? of it because of the code on my dart file?

here's the mp3 file inside the raw file



Solution 1:[1]

ShadowSheep did a good job at answering this question, but there's one thing I want to clarify for trying to get the iOS sounds to work.

You have to add the sound into XCode (which is where ShadowSheep speaks of including the asset inside of the main bundle). You can just drag and drop the audio file (in .caf or other supported format mentioned above) into the root directory (usually called Runner for Flutter) in XCode:

Xcode Image

If you have done this and follow the setup described in the above question/answer, you should be in business.

Solution 2:[2]

For me I am using the flutter_local_notifications to create the notification channel.

include this function (may create multiple notification channel)

Future<void> _createNotificationChannel(String id, String name,
String description, String sound) async {
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var androidNotificationChannel = AndroidNotificationChannel(
  id,
  name,
  description,
  sound: RawResourceAndroidNotificationSound(sound),
  playSound: true,
);

await flutterLocalNotificationsPlugin
    .resolvePlatformSpecificImplementation<
    AndroidFlutterLocalNotificationsPlugin>()
    ?.createNotificationChannel(androidNotificationChannel);}

call the function in initState: (this created 2 notification channel)

_createNotificationChannel("channel_id_1", "channel_name", "description", "alert");
_createNotificationChannel("channel_id_2", "channel_name", "description", "alarm");

Remember to save the file of alert and alarm in the res/raw in file format of .mp3.

with this payload :

{
"notification": {
    "title": "My First Notification",
    "body": "Hello, I'm push notification"
},
"data": {
    "title": "My First Notification"
},
"android": {
    "notification": {
        "channel_id": "channel_id_1"
    }
},
"to": "device_token"}

Solution 3:[3]

For the people who try to make custom sound for both android and ios good guide here:

For iOS:

To add a custom sound for iOS, add the audio clip for iOS to your project's App_Resources/iOS/ directory (iOS only accepts .wav, .aiff, and .caf extensions).

For Android:

First, add the audio clip to your project's App_Resources/Android/src/main/res/raw/ directory ((Android only accepts .wav, .mp3 and .ogg extensions).

So we can use .wav file in order to use custom sound available for both platform:

"notification": {
            "body": "Test notification",
            "title": "Test Test Test",
            "click_action": "FLUTTER_NOTIFICATION_CLICK",
           
            "sound": "your_custom_sound.wav" 
            "android_channel_id": "channel_id_youcreated",
},

          'to':
              "",
        },

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sludge
Solution 2 HitomiHoshi
Solution 3 Elmar