firebase 要禁用抖动中按钮单击时的通知声音

pxy2qtax  于 2023-08-07  发布在  其他
关注(0)|答案(2)|浏览(132)

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

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

nle07wnf

nle07wnf1#

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

字符串

ws51t4hk

ws51t4hk2#

AndroidNotificationDetail将在应用程序启动时初始化。对于SharedPreferanceClass.getNotifiationSound(),如果在应用程序启动后更改了值,则不会反映。所以请为truefalse调用不同的方法。

相关问题