Local Notifications plugin Capacitor 3

@mhartington As suggested by you, thanks.

Hi Team,

Since the announcement today, can we please have some word on whether the Local Notifications plugin now works as expected?

By expected, we mean ability to set recurring notifications:

  • Every day at time X - forever
  • Specific day of the week at time X - forever.
  • Notifications are preserved on device restart.

The current docs (at least last time I viewed) were insufficient for describing these scenarios and I and others found that these were not possible.

Please advise, thanks.

1 Like

I just implemented Local Notifications with Capacitor 3 and from what I am seeing the items that you are asking work on at least Android (emulator).

Every day at time X
This is what I am currently using. I just got it working today so haven’t confirmed that a notification continues to alert each day but they are showing up when I call getPending() after the first notification is fired. If I use Schedule.at after the notification fires, it no longer shows in getPending() as it was a one time notification.

await LocalNotifications.schedule({
    notifications: [
        {
            id: 1,
            title: 'TEST',
            schedule: {
                on: {
                    hour: 12,
                    minute: 30,
                },
                allowWhileIdle: true,
            },
        },
    ],
})

Specific day of the week at time X
ScheduleOn is a cron syntax so you should be able to do the following for every Monday at 12:30pm.

await LocalNotifications.schedule({
    notifications: [
        {
            id: 1,
            title: 'TEST',
            schedule: {
                on: {
                    day: 1, // 0-6: Sunday-Saturday
                    hour: 12,
                    minute: 30,
                },
                allowWhileIdle: true,
            },
        },
    ],
})

Notifications are preserved on device restart
I cold-booted the Android virtual device and the notifications are still there when calling getPending().

Yeah I thought it was working and had similar code to that before abandoning and using Cordova and a different syntax entirely with “trigger” etc, but I’d like to use the Capacitor implementation ideally.

I would double check your results though on a device too.

Also I think that the day parameter is actually the day in the month, not of the week from my testing anyway. So if you change the date on your device/emulator after setting up a repeating notification, see if it works as expected.

Thanks.

I am actually not currently using the day. But, looking at the source code here it looks like you are correct. To truly follow a cron job, we need two days (day of the week and day of the month). There is an open issue feat: `weekday` support in LocalNotifications · Issue #129 · ionic-team/capacitor-plugins · GitHub to add weekday.

Yeah I just re-tested too, still broken. Checked that issue, it’s been open for a YEAR.
A whole year and this still doesn’t work as expected.
My workaround which I’ve previously posted with the Cordova plugin and different syntax is fine.

Hopefully now that Capacitor 3 has been released, the team can spend time on the backlog of features/issues.

Can you point me to your previous post using the Cordova plugin?

There is an outstanding bug where if you restart the device, they get lost, so for now, I re-initialise them on app start just in case. The likelihood is that people won’t restart their phone every day so this may be almost a non issue. Just FYI.

This isn’t ideal, but is the best I can do right now until the Capacitor plugin actually works properly.

1 Like

Hi,
I am having the same issue, if I schedule notification and then call the method getPending(), it returns all the notifications. But if I close and open the app and then call the method getPending(), it returns an empty array. There is any fix or this is really an annoying bug?

My advice is to do what I’ve done:

  1. Make your own copy of the notification details in some JSON value/whatever suits you.

  2. Every time you set notifications using the plugin, also update your object above in storage.

  3. When opening the app/app init, check your own copy, if it’s there, reschedule the notifications behind the scenes without informing the user, as if it was never an issue.

I reported this plugin not working months ago and there’s no word on a fix, so I wouldn’t hold your breath.

I fixed, I updated to capacitor 3 and it’s working :smiley:

You may have fixed your issue, but I bet you the plugin still doesn’t work as per my original comment up top.

If you can reliably get repeating notifications at day X, time Y, please update this thread with your code, so everyone can benefit.

Thanks

Attention: I am using capacitor 3.
And yes, I am receiving notifications at day X, time Y.

First in a cycle I am building the array notifications
image

and then,

image

Listeners:

Works like a charm :slight_smile:

Btw, thank you for your reply.

2 Likes

Ok, looking at your code, a few things are unclear:

  1. Why is there a loop?
  2. How are you setting your day variable so that a user can choose “every Tuesday” at “14:00” or “every day” at “14:00” for example?
  3. Do these notifications appear (without opening the app) after you change your device calendar to the future and restart the device?

If you could illustrate those points then that would help everyone here.

AFAIK, in my testing of V3, point 2 above simply still isn’t even possible with how Ionic have written their plugin. Even their docs don’t illustrate it.

Thanks for the continued discussion.

  1. Because I am setting notifications between dates, and then I calculate the diff between these dates with moment(). With that I have the number of days and I loop those.

  2. The day is the initial Date that the user wants to set the notification, and then if I want every single day in 20 days, I just add 1 day with moment() in order to have all the dates schedule. I initially tested with the repeat, but don’t go for that, better sett a new schedule for every moment that you need! Use “at:”

  3. I set a new notification at 16h20 when was only 16h18, and restart my device, after boot up and the 16h20 arrived, it didn’t appear the notification, but after 6 minutes, maybe 16h26 that notification came!
    I think there is a delay to process the notification, I don’t know.

Never tested when changing the device calendar, because it’s not reasonable in my opinion.

If you need more example of code, I can give you :slight_smile:

I thank you to keep this discussion because was hard for me to found some ideas or fixes at the beginning.

It is totally reasonable to change the calendar on your device and check that it works in the future.
I would recommend you do so rather than assuming that everything is fine.

You didn’t explain how you would select specifically e.g. every Wednesday at 13:00 - please show that if you can.

Off topic - moment.js has been deprecated and datefns has been the standard replacement:
https://date-fns.org

Just in case you need it ever :slight_smile:

I will try later! :slight_smile:

Ahhh, that is a feature to do later, but I don’t have any suggestion, because we have to schedule that offline… we have to use the repeat… I don’t know… do we?

Well, thank you!! Didn’t notice that :sweat_smile: , will change!

can u help me working on same way for schedule doses

This works for me on Capacitor 3: