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

All notifications are received at once while in doze mode #2369

Closed berkaycelebi closed 3 months ago

berkaycelebi commented 4 months ago

In our app, we send notifications every 3 hours from 08:00 to 22:00 (e.g., water reminder notifications). We use zonedSchedule to achieve this.

However, many users report receiving most notifications all at once. After investigating, we found that when the device is in idle mode (Doze mode), the alarm manager doesn't work as expected, as described in the Android documentation.

Our notifications don't need to be sent at an exact time, but we expect them to be received within a few hours. In reality, users sometimes don't receive notifications for a whole day and then receive all of them at once. (mostly in android 13+)

Here is our working code:

const androidDetails = AndroidNotificationDetails(
  'Workout Daily Notification',
  'Workout Daily Notification',
  channelDescription: 'Workout Daily Notification',
  importance: Importance.max,
  enableVibration: true,
  icon: 'ic_stat_buttocks1',
  priority: Priority.high,
  styleInformation: BigTextStyleInformation(''),
);

_generalNotificationDetails = const NotificationDetails(
  android: androidDetails,
  iOS: iosDetails,
);

await _flutterLocalNotification.zonedSchedule(
  123,
  "anyString",
  "otherString",
  tz.TZDateTime.from(scheduledDate, tz.local),
  _generalNotificationDetails,
  uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
  matchDateTimeComponents: DateTimeComponents.dateAndTime,
  androidScheduleMode: AndroidScheduleMode.inexact,
  payload: 'id:${date.millisecondsSinceEpoch} scheduledDate:$scheduledDate',
);

Is it possible to set up notifications to be sent at inexact times and avoid receiving them all at once? Or what should we do to resolve the described issue?

MaikuB commented 4 months ago

Have you checked the other values for the AndroidSchedule enum? My understanding is AndroidScheduleMode.inexactAllowWhileIdle would've been what you're after

berkaycelebi commented 3 months ago

Thanks. inexactAllowWhileIdle solved the problem.

We focused on only inexact and exact and could not see other options. Thanks you for your time. I am closing this issue.

marcellocamara commented 2 weeks ago

@berkaycelebi Did you test it while app is in background?

I noticed that when the app is in background (app terminated), the notification scheduled is ok. But, when the app is in background and not terminated (app is minimized), don't show the notification scheduled, using inexactAllowWhileIdle.