ionic-team / capacitor-plugins

Official plugins for Capacitor ⚡️
518 stars 583 forks source link

Angular Ionic Local Notification using Capacitor - Daily Notification is not working #1773

Closed ionicsiva closed 9 months ago

ionicsiva commented 1 year ago

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() + '.');

}

The code above only sends out notifications at the user-selected times, but I need them sent out every day at the same time. Let me know if you have any references or suggestions.

wsamoht commented 1 year ago

For schedule I use on with success for a daily reminder:

schedule: {
    on: {
        hour: 12,
        minute: 30,
    },
    allowWhileIdle: true,
},
mia-z commented 11 months ago

Hey I'm getting a similar issue using React on Android - I'm creating a schedule which is supposed to fire via the at parameter, and then repeat via the every parameter. The documentation comments suggests these two work with eachother.

//Creating the initial schedule item, using the every-on properties.
const schedule: LocalNotificationSchema = {
    id: "Notification ID",
    title: "Reminder!",
    body: formData.note,
    actionTypeId: "",
    extra: null,
    attachments: [],
    schedule: {
        count: 54,
        allowWhileIdle: true,
        every: "week", //THIS may be the problem 
        on: {
            weekday: dayNumberFromDay(day),
            hour: parseInt(hour),
            minute: parseInt(minute),
            second: 1
        },
    },
 }

---
//Custom notification channel for Android only
const newChannel: Channel = {
    id: uuid(),
    name: "pill-buddy-notifications",
    description: "Channel for handling pill buddy notifications",
    importance: 5,
    visibility: 1,
    vibration: true,
    sound: "notif_bell.wav"
}
await LocalNotifications.createChannel(newChannel);

---
//Depending on the platform, using the previously created channel, etc.
const platform = await Capacitor.getPlatform();
if (platform === "android") {
    console.log("using channel id for android notifs: " + notificationChannelId);
    await LocalNotifications.schedule({
        notifications: [ { ...schedule, channelId: notificationChannelId } ]
    });
} else {
    await LocalNotifications.schedule({
        notifications: [ schedule ]
    });
}

This fires completely fine on iOS, however it doesnt fire at all on Android. I've tried multiple physical and emulated devices on API levels 32,33 and 34 with the same result every time. I'm also making sure to add the notification to a custom channel with maxed out notification properties. However when I comment out the every parameter, it will work properly on Android.

I haven't tested yet if it will repeat at the same time the next day, but it should according to the schema if you provide a minute and hour and put an arbitrary number on the count property.

wsamoht commented 11 months ago

The documentation says the following which I read as use one of the options, not multiple.

Use either at, on, or every to schedule notifications.

I would just use on. I am using it for both daily reminders by setting hour and minute and specific day reminders by setting weekday, hour, and minute.

mia-z commented 11 months ago

Seems I completely miseed that during my troubleshooting then. I still dont think it should stop the notifications from firing altogether, eg on Android -- that definitely feels like a bug.

wsamoht commented 11 months ago

Did a little source diving, and there is a priority in which the schedule options are used. The triggerScheduledNotification uses the first one set and then returns. My guess this is intended. Trying to handle the logic if multiple are set and merging would be a nightmare.

  1. at
  2. every
  3. on
mia-z commented 11 months ago

Ah, that does make sense. Thanks a lot for digging into that and clearing it up for us!

On Fri, 3 Nov 2023, 16:39 Tom Westrick, @.***> wrote:

Did a little source diving https://github.com/ionic-team/capacitor-plugins/blob/main/local-notifications/android/src/main/java/com/capacitorjs/plugins/localnotifications/LocalNotificationManager.java#L323, and there is a priority in which the schedule options are used. The triggerScheduledNotification uses the first one set and then returns. My guess this is intended. Trying to handle the logic if multiple are set and merging would be a nightmare.

  1. at
  2. every
  3. on

— Reply to this email directly, view it on GitHub https://github.com/ionic-team/capacitor-plugins/issues/1773#issuecomment-1792668435, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAILDNQLYCDAJEV6HVCX6CTYCUF27AVCNFSM6AAAAAA4FTVAJGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOJSGY3DQNBTGU . You are receiving this because you commented.Message ID: @.***>

ionitron-bot[bot] commented 8 months ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of the plugin, please create a new issue and ensure the template is fully filled out.