0

LocalNotificationsPlugin 应该每分钟使用不同的有效负载(变量“自定义”)调用。调用是循环进行的。我创建了插件类的一个新实例,然后使用设置对其进行初始化,以便为每个平台使用它。代码有效并显示推送通知。但是,仅显示最近的消息 -> 最后通过循环的消息。id 是根据随机数和时间唯一创建的。

为什么不能显示所有消息?非常感谢!

 //Loop and create new Push Message
    for (var i = 1; i <= final_list.length - 1; i++) {
      //Info: Index not 0 because Index 0 value should not be used

      final_message = final_list[i];


      //Add payload
      custom = final_message;


 
      
      if (i == 1 ){
        //First loop -> Selected time plus 1 min
      finalmsgtime = selectedTime.add(new Duration(minutes: 1));
      } else {
        //Second loop and bigger -> finalmsgtime + 2 min //only for test :)
        finalmsgtime = finalmsgtime.add(new Duration(minutes: 2));
      }


      //Date & Time
      var now = new DateTime.now();
      var notificationTime = new DateTime(
          now.year, now.month, now.day, finalmsgtime.hour, finalmsgtime.minute);
 
  
      //GET ID
      var randomizer = new Random(); 
      String id;
      var num_id = randomizer.nextInt(10000);
      id = '$num_id$now'; //Eindeutige ID 

      //Set push message
      scheduleNotification(
          flutterLocalNotificationsPlugin, id, custom, notificationTime);
    } //Ende Loop

在此方法中,我们创建推送消息:

Future<void> scheduleNotification(
    FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
    String id,
    String body,
    DateTime scheduledNotificationDateTime) async {
  var androidPlatformChannelSpecifics = AndroidNotificationDetails(
    id,
    'Reminder notifications',
    'Remember about it',
    icon: 'app_icon',
  );
  var iOSPlatformChannelSpecifics = IOSNotificationDetails();
  var platformChannelSpecifics = NotificationDetails(
      androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.schedule(0, 'Quote of the Day', body, //Titel von Push-Nachricht
      scheduledNotificationDateTime, platformChannelSpecifics);
}
4

1 回答 1

0

我找到了解决方案。问题是 flutterLocalNotificationsPlugin.schedule(...) 是用 ID 的静态值“0”而不是变量调用的。更改此设置后,每个通知的 ID 都是唯一的,并且通知已正确显示。

Future<void> scheduleNotification(
    FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin,
    String id,
    String body,
    DateTime scheduledNotificationDateTime) async {
  var androidPlatformChannelSpecifics = AndroidNotificationDetails(
    id,
    'Reminder notifications',
    'Remember about it',
    icon: 'app_icon',
  );
  var iOSPlatformChannelSpecifics = IOSNotificationDetails();
  var platformChannelSpecifics = NotificationDetails(
      androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);

var myID = int.parse(id);
assert(myID is int);
myID = myID - 1000;

  await flutterLocalNotificationsPlugin.schedule(myID, 'Quote of the Day', body, //Titel von Push-Nachricht
      scheduledNotificationDateTime, platformChannelSpecifics);


}
于 2020-07-30T08:01:05.873 回答