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

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

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

  1. bool isSoundEnabled = await SharedPreferanceClass.getNotifiationSound();
  2. var androidPlatformChannelSpecifics = AndroidNotificationDetails(
  3. 'channel_id',
  4. 'channel_name',
  5. enableLights: true,
  6. enableVibration: true,
  7. sound: isSoundEnabled ? const RawResourceAndroidNotificationSound("notification") : null,
  8. playSound: isSoundEnabled,
  9. icon: "@mipmap/ic_launcher",
  10. styleInformation: const BigPictureStyleInformation(FilePathAndroidBitmap(
  11. "assets/splash/splash.bmp",
  12. ),
  13. hideExpandedLargeIcon: true,
  14. ),
  15. importance: Importance.high,
  16. priority: Priority.high,
  17. );

字符串
我如何实现这个功能,或者我在上面的代码[1]中做错了什么:https://pub.dev/packages/flutter_local_notifications/install

nle07wnf

nle07wnf1#

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

  1. void showNotification(RemoteMessage message) async {
  2. final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
  3. FlutterLocalNotificationsPlugin();
  4. bool isSoundEnabled = await SharedPreferanceClass.getNotifiationSound();
  5. var androidPlatformChannelSpecificsWithSound = const AndroidNotificationDetails(
  6. 'full',
  7. 'high_importance_channel',
  8. enableLights: true,
  9. enableVibration: true,
  10. sound: RawResourceAndroidNotificationSound("notification"),//isSoundEnabled ? const RawResourceAndroidNotificationSound("notification") : null,
  11. playSound: true,
  12. icon: "@mipmap/ic_launcher",
  13. styleInformation: BigPictureStyleInformation(FilePathAndroidBitmap(
  14. "assets/splash/splash.bmp",
  15. ),
  16. hideExpandedLargeIcon: true,
  17. ),
  18. importance: Importance.high,
  19. priority: Priority.high,
  20. );
  21. var androidPlatformChannelSpecificsNoSound = const AndroidNotificationDetails(
  22. 'no_sounds',
  23. 'high_importance_channel',
  24. enableLights: true,
  25. enableVibration: true,
  26. playSound: false,
  27. icon: "@mipmap/ic_launcher",
  28. styleInformation: BigPictureStyleInformation(FilePathAndroidBitmap(
  29. "assets/splash/splash.bmp",
  30. ),
  31. hideExpandedLargeIcon: true,
  32. ),
  33. importance: Importance.high,
  34. priority: Priority.high,
  35. );
  36. // var iOSPlatformChannelSpecifics = IOSNotificationDetails();
  37. var platformChannelSpecifics = NotificationDetails(
  38. android:isSoundEnabled? androidPlatformChannelSpecificsWithSound
  39. :androidPlatformChannelSpecificsNoSound,
  40. // iOS: iOSPlatformChannelSpecifics,
  41. );
  42. await _flutterLocalNotificationsPlugin.show(
  43. 0,
  44. message.notification?.title ?? '',
  45. message.notification?.body ?? '',
  46. platformChannelSpecifics,
  47. );
  48. }

字符串

展开查看全部
ws51t4hk

ws51t4hk2#

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

相关问题