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 Notifications - stopping repeatable notifications #2561

Closed srowe0091 closed 4 years ago

srowe0091 commented 4 years ago

Bug Report

Capacitor Version

npx cap doctor output:

Installed Dependencies:
  @capacitor/ios: not installed
  @capacitor/cli: 1.5.1
  @capacitor/core: 1.5.1
  @capacitor/android: 1.5.1

Affected Platform(s)

Current Behavior

A notification that is set with the property "every" has no way of being turned off because the pending notification gets removed as soon as the first notification is triggered. Therefore losing reference to the notifications and never being able to cancel it

Expected Behavior

Have some sort of reference to active/triggered notifications that are repeated in order to properly remove them by some function/setting change.

For example, a notification that appears weekly, and allowing the user, in the app, to turn off notifications whenever they want to.

Sample Code or Sample Application Repo

LocalNotifications.schedule({
    notifications: [
      {
        title: "Title",
        body: "Body",
        id: 1,
        schedule: {
          every: 'minute'
        },
        sound: null,
        attachments: null,
        actionTypeId: "",
        extra: null
      }
    ]
  })

Reproduction Steps

calling this function

export const ScheduleNotification = async () => {
  LocalNotifications.schedule({
    notifications: [
      {
        title: "Title",
        body: "Body",
        id: 1,
        schedule: {
          every: 'minute'
        },
        sound: null,
        attachments: null,
        actionTypeId: "",
        extra: null
      }
    ]
  })
  const data = await LocalNotifications.getPending()
  console.log(data)
}

will log the output

[ { id: "1" } ]

as soon as notification is triggered, logging the list of notifications is now an empty array. I understand the function is to getPending, so maybe another function should be provided for retrieving triggered, repeatable, notifications, so they can be cancelled

Other Technical Details

npm --version output: 6.14.2

node --version output: 13.6.0

Other Information

Also, would be useful if the documentation provided a little more clarity on how to use notifications better with different scenarios.

srowe0091 commented 4 years ago

does anyone else experience this, or am i the only one?

Juarrow commented 4 years ago

You can save the id somewhere else and use that to cancel later... The following worked for me (ignore "second" and "count", since "second" seems bugged and the same as "minutes" and "count" does not seem to do anything)


async onNotification()
  {
    if(!await LocalNotifications.areEnabled())
    {
      alert('Local notifications not enabled.');
    }
    else
    {
      let notification: LocalNotification = {
        body: 'This is a fine SkekNot',
        id: 1,
        title: 'Skekification!',
        sound: null,
        schedule: {
          repeats: true,
          every: "second",
          count: 10
        },
      };

      const result = await LocalNotifications.schedule({
        notifications: [ notification ]
      });
    }
  }

async onCancelNotification()
  {
    await LocalNotifications.cancel({
      notifications: [
        { id: "1"}
      ]
    });
  }

Why one is passed as a number and the other as a string?... well... ask the people who designed this...

jcesarmobile commented 4 years ago

I've been able to reproduce, but was removed from the pending list if dismissed, not right after being triggered or if tapped. Fixed that in https://github.com/ionic-team/capacitor/pull/2809

ionitron-bot[bot] commented 1 year 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 Capacitor, please create a new issue and ensure the template is fully filled out.