skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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,
          );
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search