ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚑️
https://capacitorjs.com
MIT License
12.04k stars 999 forks source link

bug: Local notification scheduled not called #4332

Closed alphagamer7 closed 1 year ago

alphagamer7 commented 3 years ago

Bug Report

Capacitor Version

πŸ’Š   Capacitor Doctor  πŸ’Š 

Latest Dependencies:

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

Installed Dependencies:

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

[success] Android looking great! πŸ‘Œ
  Found 20 Capacitor plugins for ios:
    cordova-plugin-advanced-http (3.0.1)
    cordova-plugin-app-version (0.1.9)
    cordova-plugin-badge (0.8.8)
    cordova-plugin-device (2.0.2)
    cordova-plugin-file (6.0.2)
    cordova-plugin-file-transfer (1.7.1)
    cordova-plugin-filechooser (1.2.0)
    cordova-plugin-filepath (1.5.8)
    cordova-plugin-globalization (1.11.0)
    cordova-plugin-inappbrowser (4.0.0)
    cordova-plugin-ionic-keyboard (2.2.0)
    cordova-plugin-ionic-webview (4.2.1)
    cordova-plugin-localization-strings (3.2.1)
    cordova-plugin-nativestorage (2.3.2)
    cordova-plugin-statusbar (2.4.2)
    cordova-plugin-whitelist (1.3.3)
    cordova-sqlite-storage (5.0.1)
    cordova.plugins.diagnostic (6.0.2)
    cordova-plugin-androidx-adapter (1.1.3)
    onesignal-cordova-plugin (2.11.2)
[success] iOS looking great! πŸ‘Œ

Platform(s)

Android, iOS

Current Behavior

Schedule notification gets called only once

Expected Behavior

Schedule notification should get called daily

Code Reproduction

let notification: LocalNotification = {
            title: 'title',
            body: 'body',
            id: dailySheduleNotificationIds[i],
            schedule: {
              on: {
                hour: times[i].hour,
                minute: times[i].minute,
              },
            },
            actionTypeId: '',
          };

and the rest with LocalNotifications.schedule

Ionitron commented 3 years ago

This issue may need more information before it can be addressed. In particular, it will need a reliable Code Reproduction that demonstrates the issue.

Please see the Contributing Guide for how to create a Code Reproduction.

Thanks! Ionitron πŸ’™

danielmalmros commented 3 years ago

@alphagamer7 I have a problem similar to yours, and the documentation does not describe that well how to handle scheduled notification which repeats e.g. every day at 15:00 ("HH: mm") - at least I can not figure it out.

I got it to work on iOS with the following setup:

import { Plugins } from '@capacitor/core';
const { LocalNotifications } = Plugins;

const notifs = await LocalNotifications.schedule({
  notifications: [
    {
      title: "Title",
      body: "Body text here",
      id: uniqueId,
      schedule: {
        repeats: true,
        every: "day",
        on: { hour, minute },
      },
    },
  ]
});

However, I have not managed to get the same setup to work on Android and I am still looking for an answer on how to make it work on both iOS and Android.

So it might be a bug or else the documentation needs to be updated to better describe how to handle scheduled local notifications that repeats itself every day at a specific time.

alphagamer7 commented 3 years ago

Hi @danielmalmros, I gave up using the solution with every. Used a "hack" with scheduling "at" x number of times. Then when a user opens the app again, based on the date, rescheduling.

        // add for 14 days
        for (let j = 0; j < 14; j++) {
          var eachDay = moment(today).add(j, 'days'); 
          let notification: LocalNotification = {
            title: title,
            body: '',
            id: uniqueId
            schedule: {
              at: eachDay.toDate(),
            }
          };
        }

        return await LocalNotifications.schedule({
              notifications,
      });
danielmalmros commented 3 years ago

@alphagamer7 that would be one workaround to achieve the scheduled notifications.

But i must say I would love to hear from someone or the core team how to handle this the correct way.

I'll be watching this thread to get some clarification on this problem.

daveshirman commented 3 years ago

@alphagamer7 I have a problem similar to yours, and the documentation does not describe that well how to handle scheduled notification which repeats e.g. every day at 15:00 ("HH: mm") - at least I can not figure it out.

I got it to work on iOS with the following setup:

import { Plugins } from '@capacitor/core';
const { LocalNotifications } = Plugins;

const notifs = await LocalNotifications.schedule({
  notifications: [
    {
      title: "Title",
      body: "Body text here",
      id: uniqueId,
      schedule: {
        repeats: true,
        every: "day",
        on: { hour, minute },
      },
    },
  ]
});

However, I have not managed to get the same setup to work on Android and I am still looking for an answer on how to make it work on both iOS and Android.

So it might be a bug or else the documentation needs to be updated to better describe how to handle scheduled local notifications that repeats itself every day at a specific time.

I second this, the documentation for this does not explain even the most simple use cases, i.e.

These should be in the docs at a bare minimum.

lpil commented 3 years ago

I too am having an issue with this. It would be wonderful to get this cleared up, thank you!

DevendraDesale commented 3 years ago

@ionic-team can you please answer this question.

michaeljelly commented 2 years ago

Also having trouble! Bump!

adamiprinciples commented 2 years ago

Are there any updates on this? We've had to resort to using the Cordova plugin for this which isn't ideal as it's very poorly maintained. We're about to just write our own plugin to deal with this so just thought it was worth one last check to see if there had been any movement? We just need the 'every Thursday at 18:00' use case

daveshirman commented 2 years ago

Are there any updates on this? We've had to resort to using the Cordova plugin for this which isn't ideal as it's very poorly maintained. We're about to just write our own plugin to deal with this so just thought it was worth one last check to see if there had been any movement? We just need the 'every Thursday at 18:00' use case

If it helps, I wrote a small workaround on this using the Cordova plugin ages ago. Looks like it's a year later and this still hasn't been addressed, unless I'm mistaken?

Anyway, if you need it:

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

adamiprinciples commented 2 years ago

We were using the Cordova plugin, thanks to your blog post in fact! It does complicate the build process though if you're building on the very latest version of Android and the need to use Jetifier isn't ideal. I spotted a 1.1.0 release of the Capacitor plugin https://github.com/ionic-team/capacitor-plugins/releases/tag/%40capacitor%2Flocal-notifications%401.1.0 so am about to see if this solves my issues!

jcesarmobile commented 1 year ago

closing in favor of https://github.com/ionic-team/capacitor-plugins/issues/836 since the plugins have been moved to that repository