Flutter LocalNotificationsPlugin - 在 for 循环中调用时仅显示最新消息

Flutter LocalNotificationsPlugin - Only newest message is displayed, when called in for-loop

LocalNotificationsPlugin 应该每分钟用不同的负载(变量“自定义”)调用。调用是在循环中进行的。我创建了插件 class 的新实例,然后使用设置对其进行初始化以将其用于每个平台。代码有效并显示推送通知。但是,只显示最近的消息 -> 最后通过循环的消息。 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);
}

我找到了解决方案。问题是 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);


}