To disable notification sound on button click in flutter

huangapple go评论74阅读模式
英文:

To disable notification sound on button click in flutter

问题

我需要在按钮点击时禁用通知声音。我已经使用了[flutter_local_notification][1]包。我做的是将按钮点击的布尔值存储在本地数据库中。当推送通知到达时,它将从本地数据库中获取声音是否启用的信息,并将该值分配给播放声音。

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,
);

如何实现这个功能或者上面的代码有什么问题吗?
[1]: https://pub.dev/packages/flutter_local_notifications/install

英文:

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

答案1

得分: 1

要能够动态更改通知声音,您需要为声音和静音模式创建不同的通知通道。在Android 8.0或更新版本中创建通道后无法更新它们。当按钮被点击时,根据声音状态更改使用的通知通道。

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,
    );
}
英文:

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,
      );
    }

答案2

得分: 0

AndroidNotificationDetail 在应用启动时将被初始化。使用 SharedPreferanceClass.getNotifiationSound(),如果在应用启动后更改了该值,它将不会反映出来。因此,请为 truefalse 分别调用不同的方法。

英文:

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.

huangapple
  • 本文由 发表于 2023年7月17日 17:40:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703194.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定