我需要禁用按钮点击时的通知声音.我已经使用了[Ffltter_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

推荐答案

为了能够动态更改通知声音,您需要为声音和静音模式创建不同的通知频道.频道在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,
      );
    }

Flutter相关问答推荐

Android元数据为1.9.0,预期版本为1.7.1,如何解决flutter应用程序构建失败问题?

获取屏幕大小的滚动视图与动态大小?

如何处理Flutter Riverpod异步通知器中的状态和数据库

Flutter /dart 列表.第一个子类列表中的Where()错误

无法在仿真器上运行Flutter 项目-文件名太长错误

参数类型';List<;Dynamic&>不能分配给参数类型';List<;SingleChildWidget&>';

Flutter Android Google Sign-in error:ApiException:10在确认软件包名称、SHA-1 fingerprint 和设置同意页面后出现异常

如何正确地将 PageView 设置为 AppBar 的标题?

什么最接近 Dart 的 List.asMap().forEach() 返回列表?

转换函数上传文件上传Uint8List

如何更改中心的圆形进度指示器

Flutter Bloc 使用 Cubit 监听流和emits 状态

Flutter中如何连续获取TextField的文本是否为空?

type '({bool growable}) => List' 不是类型转换中类型 'List' 的子类型

如何限制用户每 24 小时执行的操作数?

在 Flutter 中将提取的 Listview.builder 与两个列表一起使用

我已经删除了我的 .android 密钥库,但我没有使用它,如何生成一个新的?

Flutter 小部件中的视图和逻辑分离

在 Flutter 应用程序中全局使用 assets 文件夹

Flutter audioplayers错误-单击音频按钮时不播放音频