简体   繁体   中英

Custom sound not working if we make release build in flutter app

Everything is working fine in debug mode and if I run, flutter run --release. But custom sound is not working if I generate release build and running using that. I am using flutter.

here is the code,

Future _showNotificationWithSound(title, message) async {
    var vibrationPattern = Int64List(4);
    vibrationPattern[0] = 0;
    vibrationPattern[1] = 1000;
    vibrationPattern[2] = 5000;
    vibrationPattern[3] = 2000;

    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'added_my_channel_id',
        'abc',
        'abc',
        icon: 'app_icon',
        playSound: true,
        sound: RawResourceAndroidNotificationSound('notifiysound'),
        vibrationPattern: vibrationPattern,
        enableLights: true,
        color: const Color.fromARGB(255, 255, 0, 0),
        ledColor: const Color.fromARGB(255, 255, 0, 0),
        ledOnMs: 1000,
        ledOffMs: 500);

    var iOSPlatformChannelSpecifics =
    IOSNotificationDetails(sound: 'notifiysound.mp3');
    var platformChannelSpecifics = NotificationDetails(
        androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
    flutterLocalNotificationsPlugin. show(
        0, title, message,platformChannelSpecifics);
  }

You're probably missing a keep.xml in raw folder. Then the files won't be included in build but only available when running in debug mode. See last answer here: Flutter local notification custom sound doesn't work (path issue)

Add full path instead of file name

Incorret: IOSNotificationDetails(sound: 'notifiysound.mp3');

Correct: IOSNotificationDetails(sound: 'assets/notifiysound.mp3');

please put below code in your MainActivity.java file in android folder.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
{
  Uri soundUri=Uri.parse("android.resource://"+getApplicationContext()
                    .getPackageName() + "/" +  R.raw.alert);
  AudioAttributes audioAttributes =AudioAttributes.Builder()
                  .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                  .setUsage(AudioAttributes.USAGE_ALARM)
                  .build();
  NotificationChannel channel = new 
  NotificationChannel("channelId","channelName", 
  NotificationManager.IMPORTANCE_HIGH);
  channel.setSound(soundUri, audioAttributes);
  NotificationManager notificationManager = 
  getSystemService(NotificationManager.class);
  notificationManager.createNotificationChannel(channel);
 } 

and put your Custom sound mp3 file in your projects android/app/src/raw/mp3 file

Note: it will only work for Android custom sound

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM