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

Issue

The LocalNotificationsPlugin should be called up every minute with a different payload (variable "custom"). The call is made in a loop. I create a new instance of the plugin class and then initialise it with the settings to use it for each platform. The code works and push notifications are displayed. However, only the most recent message is shown -> the message that last went through the loop. The id is created uniquely from a random number and the time.

Why could not all messages be displayed?
Thanks a lot!

 //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

In this method we create the push message:

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);
}

Solution

I found the solution. The Problem was that flutterLocalNotificationsPlugin.schedule(…) was called with the static value ‘0’ for ID and not with the variable. After I changed this, the ID for each notification was unique and the notifications have been displayed correctly.

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);


}

Answered By – tim_mx

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.