NativeScript / plugins

@nativescript plugins to help with your developments.
https://docs.nativescript.org/plugins/index.html
Apache License 2.0
189 stars 107 forks source link

[@nativescript/local-notifications] Scheduling a notification using the "at" property results in the notification being severly late #529

Open prophetofxenu opened 11 months ago

prophetofxenu commented 11 months ago

This has not been tested on iOS yet, but will be soon, and I'll update this issue with the results once that has been done. It has been replicated on both a Samsung phone and tablet.

While trying to make use of the LocalNotifications.schedule() functionality, the notification arrives as expected, but ends up being significantly late. For instance, when trying to schedule a notification a minute into the future:

public async addScheduledNotification(notificationId: string) {
    const notification = NOTIFICATION_TYPES.find((n => n.id == notificationId));
    const targetStart = new Date(new Date().getTime() + 60 * 1000);
    console.log(`target start: ${targetStart}`);
    console.log(`getTime: ${targetStart.getTime()}`);
    console.log(await LocalNotifications.schedule([{
        id: Number.parseInt(notification.id),
        title: 'My App',
        body: notification.body,
        // interval: 'day' as ScheduleInterval,
        channel: 'MyApp',
        priority: 2,
        at: targetStart
    }]));
    console.log(await LocalNotifications.getScheduledIds());
}

Output:

  JS: CONSOLE LOG: target start: Tue Sep 05 2023 21:30:00 GMT-0400 (EDT)
  JS: CONSOLE LOG: getTime: 1693963800000
  JS: CONSOLE LOG: [0]
  JS: CONSOLE LOG: [0]

the notification will arrive about 1 minute and forty five seconds from the time the code runs. I've tried different use cases and the delay can be as large as 15 minutes to an hour. Lowering the delay to ten seconds after the code runs results in it arriving on time. Leaving the at parameter out results in the notification being shown immediately as expected. There doesn't seem to be much of a difference whether the screen is on or off when the notification is supposed to fire. Seeing as the screen is often on and there's still a delay, I don't suspect the issue is due to a battery saver or Doze.

From my reading of the Android documentation and this plugin's source code, exact alarms are being used with RTC_WAKEUP so there shouldn't be much of a delay in the showing of the notification.

edusperoni commented 11 months ago

I do have a fork of this plugin that I use on an app which requires exact notification time, but it also has a few other things that are specific to that app which are difficult to implement in a generic way which is why I haven't opened a PR for it. Once I'm not in mobile I can find it and maybe PR part of it

prophetofxenu commented 11 months ago

@edusperoni okay, so you're saying this plugin is not expected to deliver notifications using precise scheduling? Or it is but doesn't work for you either, leading you to create your fork?

edusperoni commented 11 months ago

It's currently not able to deliver notifications in the correct time due to the app hibernating, as you correctly identified in the issue. I have a branch in my fork that solves that by scheduling in a way that wakes the app up immediately, that's what I meant

prophetofxenu commented 11 months ago

After more testing on emulated Pixels and a Moto, the notifications appear in a reasonable time. Not sure if there is weird behavior with Samsung devices specifically.