flutternotifications

Flutter local notification custom sound doesn't work (path issue)



I want setting up a custom sound for my local notification in the Android side. I created a raw folder in the res folder of Android project. This is the part of my code which I put my alarm.mp3 file path in the notification.
var androidPlatformChannelSpecifics =
                    new AndroidNotificationDetails(
                        "$id",
                        not.columnValue + not.deveui,
                        not.columnValue + not.deveui,
                        sound: "@raw/alarm",
                        importance: Importance.Max,
                        priority: Priority.High);

Actually it still sounds with the default system sound.

If I put @raw/alarm.mp3, I got the following exception: Unhandled Exception: PlatformException(INVALID_SOUND, The resource %s could not be found. Please make sure it has been added as a raw resource to your Android head project., null)

note : sound: RawResourceAndroidNotificationSound('alarm'), solved


Solution

  • thanks for sunnyque for giving awesome solution, but here's how i implemented it into my fcm,

    first : you have to set "channel_id","playSound: true", "sound: RawResourceAndroidNotificationSound('yourmp3files.mp3')": adding specific channel on flutter_local_notification

    Future displayNotification(Map<String, dynamic> message) async {
        var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
          'you_can_name_it_whatever',
          'flutterfcm',
          'flutterfcm',
          playSound: true,
          sound: RawResourceAndroidNotificationSound('yourmp3files.mp3'),
          importance: Importance.max,
          priority: Priority.high,
        );
    

    then, add mp3 file into res/raw/yourmp3files.mp3 adding mp3

    after that you need to add keep.xml inside res/raw/keep.xml to include yourmp3files.mp3 into build keep.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:tools="http://schemas.android.com/tools"
        tools:keep="@raw/yourmp3files"
        />
    

    your done, now you can test the fcm custom sound in your app, do

    uninstal last build

    flutter clean

    flutter pub get

    flutter build apk

    flutter run --release

    tada 🎉, tell me if its work, hehe

    i hope this help someone in the future 😁