1

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

Akhil
  • 419
  • 5
  • 15

2 Answers2

1

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.

 void showNotification(RemoteMessage message) async {
      final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
          FlutterLocalNotificationsPlugin();
      bool isSoundEnabled = await SharedPreferanceClass.getNotifiationSound();
      var androidPlatformChannelSpecificsWithSound =  const AndroidNotificationDetails(
        'full',
        'high_importance_channel',
        enableLights: true,
        enableVibration: true,
        sound: RawResourceAndroidNotificationSound("notification"),//isSoundEnabled ? const RawResourceAndroidNotificationSound("notification") : null,
        playSound: true,
        icon: "@mipmap/ic_launcher",
        styleInformation: BigPictureStyleInformation(FilePathAndroidBitmap(
          "assets/splash/splash.bmp",
        ),
          hideExpandedLargeIcon: true,
    
        ),
        importance: Importance.high,
        priority: Priority.high,
      );
      var androidPlatformChannelSpecificsNoSound =  const AndroidNotificationDetails(
        'no_sounds',
        'high_importance_channel',
        enableLights: true,
        enableVibration: true,
        playSound: false,
        icon: "@mipmap/ic_launcher",
        styleInformation: BigPictureStyleInformation(FilePathAndroidBitmap(
          "assets/splash/splash.bmp",
        ),
          hideExpandedLargeIcon: true,
    
        ),
        importance: Importance.high,
        priority: Priority.high,
      );
      // var iOSPlatformChannelSpecifics = IOSNotificationDetails();
      var platformChannelSpecifics = NotificationDetails(
        android:isSoundEnabled? androidPlatformChannelSpecificsWithSound
            :androidPlatformChannelSpecificsNoSound,
        // iOS: iOSPlatformChannelSpecifics,
      );
    
      await _flutterLocalNotificationsPlugin.show(
        0,
        message.notification?.title ?? '',
        message.notification?.body ?? '',
        platformChannelSpecifics,
      );
    }
Arjun Ranjith
  • 209
  • 1
  • 5
0

AndroidNotificationDetail will be initialized when app the starts. With SharedPreferanceClass.getNotifiationSound(), if the value is changed after the app starts, it won't reflect. So please invoke different methods for true and false.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • 1
    Thanks for the response, I have tried it by adding a breakpoint on the sound property and it's getting called on every new notification comes and the isSoundEnabled variable is getting the newly changed variable. Even though the notification sound is still there. @adil could you make it clearer or provide any supporting links – Akhil Jul 17 '23 at 11:35