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
            });
2 Likes