capacitor-plugins icon indicating copy to clipboard operation
capacitor-plugins copied to clipboard

LocalNotifications: introduce `startTime` for `every` (intervals)

Open haschu opened this issue 6 years ago • 24 comments

It would be nice if one could schedule an interval notification (e.g. every day) but with a custom startTime.

Consider the following example:

 notifications: [{
   // ....
   schedule: {
     every: 'day'
    }
   // ....
 }]

This schedules a notification that triggers every day from NOW on. So if one would schedule a notification at 11:00 am, it would trigger the next day at 11:00 am, and so on...

What I'm looking for is something like "remind every day on 08:00 am", so for example:

notifications: [{
  // ....
  schedule: {
    every: 'day',
    startDate: aSpecificStartDate
   }
  // ....
}]

This should be fairly easy to implement. Looking at the Android source, startTime needs to be configurable. (For the swift sources is's almost the same)

I would suggest to introduce a new property startTime for the schedule object. For me, this seems to be the most straightforward way, plus is stays backwards compatible.

Any thoughts on this? If you guys are okay with that solution I would give it a try and submit a pull request.

Or maybe am I missing something and there is a workaround for my problem?

haschu avatar May 24 '19 10:05 haschu

Hi, I got the same issue here. Daily reminders at a specific time would be awesome!

woelfners-miracles avatar May 24 '19 12:05 woelfners-miracles

Hi. You can already do that with on.

"remind every day on 08:00 am"

        schedule: {
          on: {
            hour: 8,
            minute: 0
          }
        }

"remind every day on 11:00 am"

        schedule: {
          on: {
            hour: 11,
            minute: 0
          }
        }

"remind every month on the 17th on 7:00 am"

        schedule: {
          on: {
            day: 17
            hour: 7,
            minute: 0
          }
        }

ralscha avatar Jun 18 '19 05:06 ralscha

@ralscha Ah, thanks! Going to try that out :)

haschu avatar Jun 20 '19 09:06 haschu

I have a similar problem. The documentation is unfortunately insufficient. I would like to send notifications every Monday at 20:00, is that possible?

jansgescheit avatar Aug 14 '19 19:08 jansgescheit

@Jannnnnn Don't see a way to do that with the current implementation. A startTime for every would solve that. Or if on would support a day of week property, like cron.

ralscha avatar Aug 15 '19 03:08 ralscha

@ralscha I went through the source code and come to the same conclusion. I have now switched back to the cordova plugin https://github.com/katzer/cordova-plugin-local-notifications

jansgescheit avatar Aug 15 '19 07:08 jansgescheit

Looking for the ability to schedule a notification on a specific day of the week every week like @Jannnnnn mentioned. Any other thoughts on how to achieve this?

askilondz avatar Aug 29 '19 21:08 askilondz

I think this an important feature for notifications, this issue is also the same as #2752

slevy85 avatar May 09 '20 13:05 slevy85

@ralscha I went through the source code and come to the same conclusion. I have now switched back to the cordova plugin https://github.com/katzer/cordova-plugin-local-notifications

I have used this plugin for long. Is very buggy, not reliable, be careful and make a lot of test.

elnezah avatar Jan 22 '21 12:01 elnezah

I have the same problem as many here: want to place a weekly notification on a certain time of the day. Anybody knows what the parameter count is for? What happens if you combine every and on?

There is a huge lack of documentation here.

elnezah avatar Jan 22 '21 12:01 elnezah

Bump

alphagamer7 avatar Mar 08 '21 12:03 alphagamer7

+1 - I have been struggling with this issue for days. I can't find any relevant information on how to do this. I see a lot of different questions and issue about this, but no feedback from the core team?

the documentation says it's possible, but from what I can tell it dose not seem to be the case for daily or weekly reminders.

danielmalmros avatar Mar 25 '21 19:03 danielmalmros

@mlynch @jcesarmobile

The documentation for this (V3) is extremely lacking. Please can someone update it to provide a way to:

  1. Schedule a DAILY notification at a specific time, e.g. Every DAY at 15:00
  2. Schedule a WEEKLY notification at a specific time, e.g. Every WEEK at 15:00
  3. Schedule a SPECIFIC DAY OF THE WEEK notification at a specific time, e.g. Every WEDNESDAY at 15:00

This is what we are trying to work out. The silence from the team is extremely frustrating. We want to use the Capacitor plugins, but if they aren't properly documented, how are we supposed to use and trust them over old Cordova ones?

Thanks.

daveshirman avatar Mar 27 '21 13:03 daveshirman

+1 - I have been struggling with this issue for days. I can't find any relevant information on how to do this. I see a lot of different questions and issue about this, but no feedback from the core team?

the documentation says it's possible, but from what I can tell it dose not seem to be the case for daily or weekly reminders.

I've only tested this on Android (7, 9, 11) - but feel free to do what I did here, it's a workaround, but it works in my testing so far:

https://www.codingandclimbing.co.uk/blog/android-localnotifications-with-cordova-capacitor-example

daveshirman avatar Mar 28 '21 17:03 daveshirman

+1 - I have been struggling with this issue for days. I can't find any relevant information on how to do this. I see a lot of different questions and issue about this, but no feedback from the core team? the documentation says it's possible, but from what I can tell it dose not seem to be the case for daily or weekly reminders.

I've only tested this on Android (7, 9, 11) - but feel free to do what I did here, it's a workaround, but it works in my testing so far:

https://www.codingandclimbing.co.uk/blog/android-localnotifications-with-cordova-capacitor-example

@daveshirman amazing job with that blog post! Maybe just mention that it only seems to work with the 0.9.0-beta.3 cordova plugin. Looks like npm is installing the 0.9.0-beta.2 version. I'll do some more testing on both Android and iOS tomorrow to see if it helps with the daily reminders.

With that said, I still hope to see some clarification around the daily reminders from the Ionic team on the @capacitor/local-notifications plugin 🙏

danielmalmros avatar Mar 28 '21 19:03 danielmalmros

+1 - I have been struggling with this issue for days. I can't find any relevant information on how to do this. I see a lot of different questions and issue about this, but no feedback from the core team? the documentation says it's possible, but from what I can tell it dose not seem to be the case for daily or weekly reminders.

I've only tested this on Android (7, 9, 11) - but feel free to do what I did here, it's a workaround, but it works in my testing so far: https://www.codingandclimbing.co.uk/blog/android-localnotifications-with-cordova-capacitor-example

@daveshirman amazing job with that blog post! Maybe just mention that it only seems to work with the 0.9.0-beta.3 cordova plugin. Looks like npm is installing the 0.9.0-beta.2 version. I'll do some more testing on both Android and iOS tomorrow to see if it helps with the daily reminders.

With that said, I still hope to see some clarification around the daily reminders from the Ionic team on the @capacitor/local-notifications plugin 🙏

Have updated my post to reference exact plugin url I used, hint it's not the katzer one that appears everywhere:

npm install https://github.com/Steffaan/cordova-plugin-local-notifications.git;

@danielmalmros

EDIT: The notification didn't trigger for the consecutive days, so I've updated my post with count in the trigger, i.e:

trigger: { every: { hour: hours, minute: mins }, count: 365 },

Manually changing the date + time seems to trigger it, but will do more testing letting the phone do its thing over the next few days.

Any other info your side?

daveshirman avatar Mar 28 '21 19:03 daveshirman

Is it possible you look into this? I am fairly certain it would be easy on the android side at least.

Just make it possible to combine at with every or on.

The current interval for repeating notifications using at does not make any sense https://github.com/ionic-team/capacitor-plugins/blob/main/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java#L348

I would suggest to just use the at parameter as a startTime for the repeating alarm when every or on is set.

https://github.com/ionic-team/capacitor-plugins/blob/main/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java#L365 https://github.com/ionic-team/capacitor-plugins/blob/main/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java#L374

For iOS we would have to do something similar.

Any reason against this?

digaus avatar Apr 22 '21 21:04 digaus

bumping this up. As users of ionic, we would like to be able to setup a daily notification at a specific time. (and a weekly one on a specific day and specific time)

the source code https://github.com/ionic-team/capacitor-plugins/blob/main/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java#L340

does not seem to support that functionality.

If we use "every": day", we can't specify the start time. (kinda important, don't you think?) source code does this: long startTime = new Date().getTime() + everyInterval; should be long startTime = schedule.getAt() // but if there's an at specified, we don't get this far in the code.

if we use "on:" new Date(year, month, day, hour, minute, second) and "repeat", it repeats once a year. (or can we use count 365?)

The original poster covered it pretty clearly. We spent 2 days on this before digging into the source code.

tomeberhard avatar Nov 03 '21 18:11 tomeberhard

Would love to see this supported!

galaxyblur avatar May 03 '22 02:05 galaxyblur

bumping this up. As users of ionic, we would like to be able to setup a daily notification at a specific time. (and a weekly one on a specific day and specific time)

the source code https://github.com/ionic-team/capacitor-plugins/blob/main/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java#L340

does not seem to support that functionality.

If we use "every": day", we can't specify the start time. (kinda important, don't you think?) source code does this: long startTime = new Date().getTime() + everyInterval; should be long startTime = schedule.getAt() // but if there's an at specified, we don't get this far in the code.

if we use "on:" new Date(year, month, day, hour, minute, second) and "repeat", it repeats once a year. (or can we use count 365?)

The original poster covered it pretty clearly. We spent 2 days on this before digging into the source code.

I have the same issue as many others here: I want to build a daily notification on which time is selected in frontend and want to execute a daily notification at the same time every day. I tried the repeats: true and every: 'day' here, but it didn't work for me. I tried this and searched everywhere.

I'm Using Ionic with angular on that am Using the Local Notification capacitor plugin.

Look for my code below.

import { LocalNotifications } from '@capacitor/local-notifications';

async setAlarm() { const now = new Date(); const alarmDate = new Date(this.alarmTime); const alarmTime = new Date(); alarmDate.setFullYear(now.getFullYear(), now.getMonth(), now.getDate());

if (alarmDate <= now) { alarmDate.setDate(alarmDate.getDate() + 1); }

await LocalNotifications.schedule({

notifications: [ { title: 'Alarm', body: 'Time to wake up!', id: 1, schedule: { at: new Date(this.alarmTime), repeats: true, every: 'day', }, actionTypeId: '', extra: null, }, ], });

alert('Alarm set for ' + alarmDate.toLocaleTimeString() + '.'); }

ionicsiva avatar Aug 31 '23 10:08 ionicsiva

@ionicsiva we had the exact same issue on our end. I really hope this is something the Capacitor team will prioritise.

After spending days trying to solve this and even play around with source code, we decided to go with a backend implementation and OneSignal for all of our notifications. But with that said, the local notification should something that is a core part of Capacitor.

danielmalmros avatar Aug 31 '23 13:08 danielmalmros

Daily Notifications should work. In my app the user can set a weekday and a time when notification should be sent, e.g.

import { LocalNotificationSchema, LocalNotifications, Weekday } from '@capacitor/local-notifications';


const notifyAt = new Date();
const notifications: LocalNotificationSchema = [
    Weekday.Monday,
    Weekday.Tuesday,
    Weekday.Wednesday,
    Weekday.Thursday,
    Weekday.Friday,
    Weekday.Saturday,
    Weekday.Sunday,
]
.map(weekday => {
    return {
        title: 'some title',
        body: 'some body',
        id: weekday,
        schedule: {
            on: {
                weekday,
                minute: notifyAt.getMinutes(),
                second: 0,
                hour: notifyAt.getHours(),
            },
        },
    };
});

await LocalNotifications.schedule({ notifications })

jansgescheit avatar Aug 31 '23 13:08 jansgescheit

Daily Notifications should work. In my app the user can set a weekday and a time when notification should be sent, e.g.

import { LocalNotificationSchema, LocalNotifications, Weekday } from '@capacitor/local-notifications';


const notifyAt = new Date();
const notifications: LocalNotificationSchema = [
    Weekday.Monday,
    Weekday.Tuesday,
    Weekday.Wednesday,
    Weekday.Thursday,
    Weekday.Friday,
    Weekday.Saturday,
    Weekday.Sunday,
]
.map(weekday => {
    return {
        title: 'some title',
        body: 'some body',
        id: weekday,
        schedule: {
            on: {
                weekday,
                minute: notifyAt.getMinutes(),
                second: 0,
                hour: notifyAt.getHours(),
            },
        },
    };
});

await LocalNotifications.schedule({ notifications })

Thank you for your assistance; it's working. Sending love from India!👍❤️

ionicsiva avatar Sep 02 '23 13:09 ionicsiva