(Android) Ionic local notifications are not delivered on time

I try to use @capacitor/local-notifications with Ionic 7 and React. For iOS the notifications work perfectly, but for Android the notifications arrive irregular. Sometimes the message arrives 10min too late, sometimes even 70min too late. It seems correlated with the screen state: when I wake up the phone, the notifications arrives a little later.

I added the SCHEDULE_EXACT_ALARM permission.
I use the allowWhileIdle: true attribute.

My code:

async function createLocalNotifications() {
    const check = await LocalNotifications.checkPermissions(); // {receive: "granted"}
    const res = await LocalNotifications.requestPermissions(); // {display:"granted"}
    if (res.display === "granted") {
      LocalNotifications.createChannel({
        id: "1",
        name: "channel_name",
        description: "channel_description",
      })
        .then(() =>
          LocalNotifications.schedule({
            notifications: [
              {
                channelId: "1",
                id: 1,
                title: `Daily notification`,
                body: "at 16:00",
                schedule: {
                  on: { hour: 16 },
                  allowWhileIdle: true,
                },
              },
            ],
          })
        )
    }
  }

What could be the problem?

I’ve noticed this too. If my Android phone hasn’t been active for a bit (probably in Doze), local notifications are not fired at the exact time they were scheduled.

Reading through Schedule alarms  |  Background work  |  Android Developers, it seems this is expected when using SCHEDULE_EXACT_ALARM and setExactAndAllowWhileIdle (allowWhileIdle).

It sounds like to always get exact alarms/notifications, you need to use USE_EXACT_ALARM; however, this has restrictions by Android so it depends on your use case and whether or not it fits under Android’s guidelines.

These notes for the release of Android 14 also gives a good summary - Schedule exact alarms are denied by default  |  Android Developers.

Amazing! I added <uses-permission android:name="android.permission.USE_EXACT_ALARM" />
Now it works perfectly. Thanks a lot!

1 Like

Nice! I wonder if I should switch to it too. My app uses local notifications for reminders. I am just not sure if Google will accept that as a valid use case for that permission :thinking:

I will bring it to the Play Store with the next update… I guess they are not too strict… :+1: