Add mp3 sound in flutter_local_notifications

8,212

Solution 1

In the documentation, there is a lot of information about how you have to handle custom sounds. You need to configure the resource in the android folder and use the config set the sound. Also, you may need to check. how to. work with notification channels. In the source code, there is an example folder with a demonstration of this:

var androidPlatformChannelSpecifics = AndroidNotificationDetails(
    'your other channel id',
    'your other channel name',
    'your other channel description',
    icon: 'secondary_icon',
    sound: 'slow_spring_board',
    largeIcon: 'sample_large_icon',
    largeIconBitmapSource: BitmapSource.Drawable,
    vibrationPattern: vibrationPattern,
    enableLights: true,
    color: const Color.fromARGB(255, 255, 0, 0),
    ledColor: const Color.fromARGB(255, 255, 0, 0),
    ledOnMs: 1000,
    ledOffMs: 500);

You can check the full example at https://github.com/MaikuB/flutter_local_notifications/blob/master/flutter_local_notifications/example/lib/main.dart

Solution 2

Add raw folder in android project : android>app>src>main>res> New Android Resource Directory > Select Raw > add your sound here

Result Folder :
- android
- app
  - src 
    - main
      - res
        - raw
            lawgo_sound_notification.mp3

image here : [1]: https://i.stack.imgur.com/EHquv.png

implement your local notification code:

var androidPlatformChannel = new AndroidNotificationDetails(
    "your_channel_id", "name", "desc_channel",
    sound: RawResourceAndroidNotificationSound('lawgo_sound_notification'),
    playSound: true,
    importance: Importance.Max,
    priority: Priority.High);

add onResume, OnLaunch support, when apps run on background notification payload should be setup to this:

{ "notification" : {
     "body" : "Body of Your Notification",
     "title": "Title of Your Notification",
     "android_channel_id": "your_channel_id"
 },
 "data" : {
     "body" : "Body of Your Notification in Data",
     "title": "Title of Your Notification in Title",
     "click_action": "FLUTTER_NOTIFICATION_CLICK",
 }
}

**important should add "android_channel_id": "default_notification_channel_id"**
Share:
8,212
SOUGAT RIYADH
Author by

SOUGAT RIYADH

Updated on December 16, 2022

Comments

  • SOUGAT RIYADH
    SOUGAT RIYADH over 1 year

    How to add custom mp3 sound in flutter_local_notification, there is a feature of adding custom ringtone but unfortunately no documentation or examples.