Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: local notifications custom sound doesn't work

I'm trying to play my own custom sound for my notifications. But on my android emulator, it just plays the default sound and on my own device, it doesn't even make a sound. I tried this but it didn't help me.


FlutterLocalNotificationsPlugin _notifications = FlutterLocalNotificationsPlugin();

var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
var initializationSettingsIOS = IOSInitializationSettings();
var initializationSettings = InitializationSettings(initializationSettingsAndroid, initializationSettingsIOS);
await _notifications.initialize(initializationSettings);
var androidPlatformChannelSpecifics = AndroidNotificationDetails('test_channel', 'test', '', playSound: true, sound: RawResourceAndroidNotificationSound('my_sound'));
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await _notifications.show(0, 'test', 'this is a test', platformChannelSpecifics);

I don't get any errors. My file is in android/app/src/main/res/raw/my_sound.mp3

like image 998
JakesMD Avatar asked Dec 04 '22 17:12

JakesMD


2 Answers

Simple fix! You have to uninstall and reinstall the app completely instead of just updating it. The notification settings from your first install will persist, so if it was once set to default sound, that will persist if you don't change the channel id. More info here

like image 190
JakesMD Avatar answered Jan 02 '23 06:01

JakesMD


According to the doc: "For Android 8.0 or newer, this (sound) is tied to the specified channel and cannot be changed after the channel has been created for the first time."

So, either create a separate channel or just uninstall the application to delete the notification channel and reinstall it.

like image 31
Fahid sarker Avatar answered Jan 02 '23 07:01

Fahid sarker