MaikuB / flutter_local_notifications

A Flutter plugin for displaying local notifications on Android, iOS, macOS and Linux
2.47k stars 1.4k forks source link

zonedSchedule doesn't work for > 500 notifications #2231

Closed Lzyct closed 8 months ago

Lzyct commented 9 months ago

Describe the bug I've created a zonedSchedule notification with about 500 notifications. The notification is created. But it doesn't work. But when I decrease the amount of notifications to 6-30 notifications, that's working well.

image

To Reproduce

  1. Create a loop to create the zonedSchedule of about 500
  2. Observe the notification.

Expected behavior That notification should be work because there no error when creating the zoneSchedule notification.

Sample code to reproduce the problem

...

for (int index=0;index<100;index++){
  flutterLocalNotificationsPlugin.zonedSchedule(
              index + 1,
              title,
              message,
              TZDateTime.now(local).add(
                Duration(seconds: 60 * (index + 1)),
              ),
              notificationDetails,
              uiLocalNotificationDateInterpretation:
                  UILocalNotificationDateInterpretation.absoluteTime,
              androidScheduleMode: AndroidScheduleMode.alarmClock,
            );

}
nbergemann commented 9 months ago

Hello,

It looks like you're encountering the device-specific limit (500) for scheduled notifications, which is common on some Android devices. The Flutter Local Notifications plugin documentation mentions this limit, and reducing the number of notifications to below this threshold, as you did, is a common workaround.

To avoid hitting these limits, you might consider:

Here's a quick code snippet to limit the number of scheduled notifications:

int maxNotifications = 100; // Adjust based on your needs
for (int index = 0; index < maxNotifications; index++) {
  flutterLocalNotificationsPlugin.zonedSchedule(
    index + 1,
    title,
    message,
    TZDateTime.now(local).add(Duration(seconds: 60 * (index + 1))),
    notificationDetails,
    uiLocalNotificationDateInterpretation:
        UILocalNotificationDateInterpretation.absoluteTime,
    androidScheduleMode: AndroidScheduleMode.alarmClock,
  );
}

Adjust maxNotifications based on testing or device-specific limits. Hope this helps!

Lzyct commented 8 months ago

Okay, thanks @nbergemann