Flutter 本地通知声音适用于除 Samsung A50 Android 11 之外的大多数设备

标签 flutter flutter-local-notification

我想在用户打开我的 flutter 应用程序时显示推送通知。通知出现在大多数设备中,并且有声音,并且在三星和 Realme 中显示为弹出窗口,但在我的设备三星 A50 中,通知出现但没有声音,并且不弹出,仅出现在通知栏中。我在Android项目的res文件夹中创建了一个raw文件夹。

这是我的代码

 import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:rxdart/rxdart.dart';

class NotificationApi{
  static final notifications = FlutterLocalNotificationsPlugin();
  static final onNotifications = BehaviorSubject<String?>();



  static Future notificationsDetails() async {
    var androidPlatformChannelSpecifics =
    AndroidNotificationDetails(
      'Notification Channel ID',
      'Channel Name',
      importance: Importance.max,
      priority: Priority.high,
      sound: RawResourceAndroidNotificationSound('sound'),
     // ongoing: true,
    );

    var iOSPlatformChannelSpecifics = IOSNotificationDetails(
      //  sound: 'slow_spring_board.aiff'
    );

    return
    NotificationDetails(
        android: androidPlatformChannelSpecifics,
        iOS: iOSPlatformChannelSpecifics
    );

  }

  static Future init({bool initScheduled = false}) async {
    final android = AndroidInitializationSettings('@mipmap/ic_launcher');
    final ios = IOSInitializationSettings(
      requestSoundPermission: false,
      requestBadgePermission: false,
      requestAlertPermission: false,

    );
    final settings = InitializationSettings(android: android,iOS: ios);
    await notifications.initialize(
      settings,
    onSelectNotification: (payload) async {
        onNotifications.add(payload);
    });

  }

  static Future showNotification({int? id,
  String? title,
  String? body,
  String? payload}) async {
    notifications.show(id!, title, body, await notificationsDetails(),payload: payload);
  }
      

 }

在我的 home.dart

 @override
  void initState() {

    super.initState();

    NotificationApi.init();
    listenNotifications();
     /// some code
     showLocalNotification();
}


listenNotifications(){
    NotificationApi.onNotifications.stream.listen((event) {
      Navigator.of(context).push(MaterialPageRoute(builder: (context)=>InboxClass()));
    });
  }

showLocalNotification(List<OfferModel> offerList){

    for(var i=0;i<offerList.length;i++){

      NotificationApi.showNotification(title: offerList[i].NAME,body:offerList[i].DESCRIPTION,payload: "",id: i);
    }
  }

最佳答案

可能错误在于重要性参数。

重要性:Importance.max

应该是

重要性:重要性.高

Android 不使用 Importance.max,请参阅 here

关于Flutter 本地通知声音适用于除 Samsung A50 Android 11 之外的大多数设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70834735/

相关文章:

flutter - Charts_flutter软件包中的图形中的domainFn和measureFn是什么?

flutter - 如何在Flutter中使用本地JSON在我的应用程序中动态创建页面?

Flutter:创建一个 pdf 文件,显示下载通知并在用户单击通知时打开它

flutter - 如何在flutter中发送推送通知,使其不会停留在系统托盘中?或出现后从系统托盘中删除

flutter - 如何更改通知flutter_local_notification的背景颜色?

flutter - 如何使用 flutter 本地通知删除通知 channel ?

android - Flutter - 透明系统导航栏

flutter - 在ListWheelScrollView中获取子项的值

html - HTML窗口小部件中未显示本地 flutter Assets 图像