flutter_local_notifications 调度不起作用

问题描述 投票:0回答:1

flutter 本地通知可以很好地使用 mehtod show 进行即时通知 但即使在应用程序打开时 1 秒后也不会安排未来时间的通知

我初始化了通知插件,就像使用此功能一样,即时通知效果很好

  static Future initialize(FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin)async{
    var androidInitialize = new AndroidInitializationSettings('mipmap/ic_launcher');
    var initializationSettings = new InitializationSettings(android:androidInitialize,);
    await flutterLocalNotificationsPlugin.initialize(initializationSettings);
  }

我初始化了时区并指定了本地时区 我在清单中指定了频道名称“you_can_name_it_whatever1”,并添加了权限,正如我所说,通过按 btn 使用 show 方法显示通知,它可以很好地进行即时通知

这是安排通知的代码

Future zonedScheduleNotification(String note, DateTime date, occ) async {
        // IMPORTANT!!
        //tz.initializeTimeZones(); --> call this before using tz.local (ideally put it in your init state)
        
        int id = math.Random().nextInt(10000);
        print(date.toString());
        print(tz.TZDateTime.parse(tz.local, date.toString())
            .toString());
        try {
          await flutterLocalNotificationsPlugin.zonedSchedule(
            id,
            occ,
            note,
            tz.TZDateTime.parse(tz.local, date.toString()),
            NotificationDetails(
              android: AndroidNotificationDetails(
      'you_can_name_it_whatever1',
      'channel_name',

      playSound: true,
      //sound: RawResourceAndroidNotificationSound('notification'),
      importance: Importance.max,
      priority: Priority.high
    ),
            ),
            androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle ,
            uiLocalNotificationDateInterpretation:
                UILocalNotificationDateInterpretation.absoluteTime,
          );
          return id;
        } catch (e) {
          print("Error at zonedScheduleNotification----------------------------$e");
          if (e ==
              "Invalid argument (scheduledDate): Must be a date in the future: Instance of 'TZDateTime'") {
            alert(context, "Select future date");
          }
          return -1;
        }
      }

然后我使用

在 btn 中调用它
            DateTime later =  DateTime.now().add(Duration(seconds:3));;
        print('____________________________________this is later');
        print(later);
               zonedScheduleNotification('a note',later,'well');

但是没有任何反应,没有错误,没有通知OBS

抱歉,如果这是一个转储问题,我有点新

我尝试了很多不同的东西,比如玉米,使用 show 进行即时通知呼叫,它在应用程序处于活动状态或在后台时工作,但在应用程序终止时停止 我尝试了该包很棒的通知,但它不起作用

我尝试像这样调用日程通知...... 来自官方文档

await flutterLocalNotificationsPlugin.zonedSchedule(
    0,
    'scheduled title',
    'scheduled body',
    tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
    const NotificationDetails(
        android: AndroidNotificationDetails( // 
            'you_can_name_it_whatever1', 'you_can_name_it_whatever1',
            channelDescription: 'your channel description')),
    androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
    uiLocalNotificationDateInterpretation:
        UILocalNotificationDateInterpretation.absoluteTime);

我已经这样做了好几天了,如果有另一种方法可以在应用程序处于任何状态下活动空闲或终止时安排通知,请告诉我我很好用它 再次抱歉,如果这是一个转储问题,我是新的 谢谢你

flutter flutter-local-notification
1个回答
0
投票

您似乎尚未初始化时区数据库。 你把它放在初始化函数中:

tz.initializeTimeZones();
© www.soinside.com 2019 - 2024. All rights reserved.