0

我已经构建了一个待办事项列表应用程序,用户可以在其中根据特定日期和时间安排通知,当应用程序在前台时通知显示没有问题,但是当应用程序在后台或应用程序关闭时,它们不显示up,我不明白为什么,任何原因,谢谢。

  • 这是我的代码

  late FlutterLocalNotificationsPlugin _localNotificationsPlugin;
  List<Task> _list = [];
  late MoorController moorController;
  @override
  void initState() {
    super.initState();
    moorController = Get.put(MoorController());
    createNotification();
    //showNotification();
  }


  void createNotification(){
    _localNotificationsPlugin = FlutterLocalNotificationsPlugin();
    var initializationSettingsAndroid = const AndroidInitializationSettings("@mipmap/ic_launcher");
    var initializationSettingsIOs = const IOSInitializationSettings();
    var initSettings = InitializationSettings(android: initializationSettingsAndroid,iOS: initializationSettingsIOs);
    _localNotificationsPlugin.initialize(initSettings);
  }
  
  void showNotification() async {
    var android = const AndroidNotificationDetails(
        Utils.CHANNEL_ID,
        Utils.CHANNEL_NAME,
        channelDescription: Utils.DESCRIPTION,
        priority: Priority.high,
        importance: Importance.max);

    var iOS = const IOSNotificationDetails();
    var platform =  NotificationDetails(android: android,iOS: iOS);


    for (var element in moorController.tasks)  {
      if(element.date == getCurrentDate() &&  element.time == getCurrentTime()){
        await _localNotificationsPlugin.schedule(
          0,element.date,element.time,DateTime.now(),platform, payload : 'Welcome to todo app',androidAllowWhileIdle: true);
        //0, element.date,element.time,platform, payload: 'Simple Payload'
       }
     }
   }
  • 收货人清单文件

在此处输入图像描述

  • 权限

在此处输入图像描述

4

2 回答 2

0

您应该检查您的有效负载是否包含click_actionwith FLUTTER_NOTIFICATION_CLICK

{
  "notification": {
    "body": "this is a body",
    "title": "this is a title",
  },
  "data": {
    "click_action": "FLUTTER_NOTIFICATION_CLICK",
    "sound": "default", 
    "status": "done",
    "screen": "screenA",
  },
  "to": "<FCM TOKEN>"
}'
于 2022-02-18T00:48:05.683 回答
0

您是否将这些权限添加到您的AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

在应用程序部分内:

<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver"><intent-filter> 
<action android:name="android.intent.action.BOOT_COMPLETED"></action></intent-filter></receiver>
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
于 2022-02-18T04:10:20.787 回答