I need to disable the notification sounds on a button click. I have used the [flutter_local_notification][1] package. What I did that the boolean value from the button click has been stored in the local DB. When a push notification comes, it will fetch from the local db to find whether the sound is enabled and assign that value to play sound.
bool isSoundEnabled = await SharedPreferanceClass.getNotifiationSound();
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'channel_id',
'channel_name',
enableLights: true,
enableVibration: true,
sound: isSoundEnabled ? const RawResourceAndroidNotificationSound("notification") : null,
playSound: isSoundEnabled,
icon: "@mipmap/ic_launcher",
styleInformation: const BigPictureStyleInformation(FilePathAndroidBitmap(
"assets/splash/splash.bmp",
),
hideExpandedLargeIcon: true,
),
importance: Importance.high,
priority: Priority.high,
);
How can I implement this feature or is there anything I did wrong in the above code
[1]: https://pub.dev/packages/flutter_local_notifications/install
2
Answers
AndroidNotificationDetail
will be initialized when app starts.SharedPreferanceClass.getNotifiationSound()
if value changed after app start, it won’t reflect. So please invoke different method for true & false.To be able to change the notification sound dynamically, you need to create different notification channels for sound and mute modes. Channels cannot be updated after they are created in Android 8.0 or newer. When the button is clicked, change the notification channel used based on the sound status.