Local Notifications every week on specific day (Cordova or Capacitor)

Hello everyone,

I have a goal, and that goal is to have Local Notifications on my app, every week, in a specific day and time. I tried to do it with capacitor, but it doesn t seem to work, ans there is not to much doc on the web. So i decided to install th cordova plugin. But now i get an error when i build my app:

error: cannot find symbol
import android.support.v4.app.NotificationCompat;
                             ^
  symbol:   class NotificationCompat
  location: package android.support.v4.app

Do someone have an idea of how can I either do it with capacitor, or fix this error to do it with the cordova plugin.

Thanks in advance !

I really wish people would stop using vague phrases like “doesn’t work”. They convey almost zero useful information. Much more productive is describing in concrete detail:

  • what you tried
  • what it did
  • what you wanted instead
  • how those two things are different

In any event, what I would do in this situation is to pick an arbitrary number of notifications to preset based on how frequently people are expected to be using the app. Let’s say four for the sake of discussion, which would be a month’s worth.

So at every app launch, I would ask getPending() what’s in the pipeline, and if there are fewer than four in there, I would use date-fns to walk out X more weeks one at a time, filling out until we’re up to four, and calling schedule() to book each one.

Thanks for the answer, actually, the problem is that i tried a lot of configurations for the capacitor Local Notifications, so it’s hard to describe everything. Now i wanted to try with Cordova… but i get this error so I can’t, and it’s pretty hard to fix it.

The prototype of the capacitor schedule is this one :

export interface LocalNotificationSchedule {
    at?: Date;
    repeats?: boolean;
    every?: 'year' | 'month' | 'two-weeks' | 'week' | 'day' | 'hour' | 'minute' | 'second';
    count?: number;
    on?: {
        year?: number;
        month?: number;
        day?: number;
        hour?: number;
        minute?: number;
    };
}

It looks trivial, but I never succeed to had the same notification, every week, on the same day, and same time…
I’m gonna try what your method

1 Like

I didn’t realize that it had repeating notifications built in. That should be even easier then.

How about testing it by scheduling one on the same minute of every hour? That should give you quicker feedback.

How about testing it by scheduling one on the same minute of every hour? That should give you quicker feedback.

Yeah I did with ‘on’, and it’s working, but the problem is that, with ‘on’ , i can t schedule every week, if I schedule like that: on: { day: 2, hour: 15, minute: 15}, it will send a notification every 2 of a month, at 3pm 15.
And with ‘every’, i tried also but it was not working, so i tried also while setting ‘repeats’ on true, but then i had a notification every minute, and not every hour. I just scheduled again a notification with the config: {at: new Date(Date(now()+1000)), every: ‘hour’}.

I’ll see tomorow morning if it’s working

“every” looks like the best choice to me. Are you testing this on Android or iOS? It looks like the code paths are not identical.

I’m testing on android, i just tried this config:

await LocalNotifications.schedule({
          notifications: [{
              title: title,
              body: body,
              id: id,
              extra: {
                  section: section
              },
              iconColor: '#0000FF',
              schedule: {at: new Date(Date.now() + 60000) , every: 'hour'}
          }]
      });

But only the first notification is sending, so 1 minute after i schedule, but then no more notifications.

A couple of things:

Can you run and paste the output of npx cap doctor?
Is the app running the whole time you’re testing?

Things changed today, I updated my java JDK, which magicaly fixed my errors, which mean i was able to use the cordova plugin. So i successfully added the notifications as expected. But now, for a misterious reason (I didn t changed the code or de dependencies) , I have back the same errors on android studio, as on the screen:

When I’m running ‘npx cap doctor’, everything is fine:

Capacitor Doctor

Latest Dependencies:

  @capacitor/cli: 2.4.0
  @capacitor/core: 2.4.0
  @capacitor/android: 2.4.0
  @capacitor/electron: 2.4.0
  @capacitor/ios: 2.4.0

Installed Dependencies:

  @capacitor/ios not installed
  @capacitor/cli 2.4.0
  @capacitor/core 2.4.0
  @capacitor/android 2.4.0
  @capacitor/electron not installed

[success] Android looking great! �

I fixed the problem with npx jetify

So then i sheduled as follow (using weekday) and it worked :

this.localNotifications.schedule({
              id: this.notifs.notifs[i].id,
              title: this.title,
              text: this.body,
              trigger: {
                count: 1,
                every: {weekday: i, hour: new Date(this.notifs.time).getHours(), minute: new Date(this.notifs.time).getMinutes()}
              },
              data: {page: this.page},
              foreground: true
            });