katzer / cordova-plugin-local-notifications

Cordova Local-Notification Plugin
Apache License 2.0
2.57k stars 1.75k forks source link

Every triggers countless notifications for a minute, then stops - 0.9-beta.3 #1533

Closed alesgenova closed 1 week ago

alesgenova commented 6 years ago

WARNING: IF YOU IGNORE THIS TEMPLATE, WE'LL IGNORE YOUR ISSUE. YOU MUST FILL THIS IN!

Provide a general summary of the issue.

Your Environment

Expected Behavior

There should be a single notification at the specified time, and the nothing until the following minute/hour/day/week

Actual Behavior

I'm trying to get a daily/weekly notification, but I ran into a bug. When the matching time comes, notifications start firing (i.e. more than one notification per second), until the clock of the phone changes. I fround this issue both using the every match, and every repeat

Steps to Reproduce


// Something like this
let notifications = [
      {
        id: 0,
        title: "title",
        text: "text",
        icon: "",
        smallIcon: "res://"+this.notificationStrings.icon,
        wakeup: false,
        trigger: {
          every: {hour: at.getHours(), minute: at.getMinutes()},
        }
      }
]

// Or even

let notifications = [
      {
        id: 0,
        title: "title",
        text: "text",
        icon: "",
        smallIcon: "res://"+this.notificationStrings.icon,
        wakeup: false,
        trigger: {
          firstAt: at,
          every: "minute",
        }
      }
]

cordova.plugins.notification.local.schedule(notifications,()=>{
      console.log("notification set ", at);
    });

Context

Debug logs

02-13 12:59:00.492  1634  1634 E NotificationService: Muting recently noisy 0|com.tapabit.zest|0|null|10079
02-13 12:59:00.705  1634  1634 I chatty  : uid=1000 system_server identical 29 lines
02-13 12:59:00.710  1634  1634 E NotificationService: Muting recently noisy 0|com.tapabit.zest|0|null|10079
02-13 12:59:00.717  1634  8564 E NotificationService: Package enqueue rate is 5.6015325. Shedding 0|com.tapabit.zest|0|null|10079. package=com.tapabit.zest
02-13 12:59:00.830  1634  1634 E NotificationService: Muting recently noisy 0|com.tapabit.zest|0|null|10079
02-13 12:59:01.833  1634  1634 I chatty  : uid=1000 system_server identical 4 lines
02-13 12:59:02.036  1634  1634 E NotificationService: Muting recently noisy 0|com.tapabit.zest|0|null|10079
02-13 12:59:02.172  2241  9000 E ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded
02-13 12:59:02.238  1634  1634 E NotificationService: Muting recently noisy 0|com.tapabit.zest|0|null|10079
--------- beginning of main
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver.onReceive(AbstractTriggerReceiver.java:65)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.trigger(Notification.java:254)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.schedule(Notification.java:206)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Manager.schedule(Manager.java:100)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.localnotification.TriggerReceiver.onTrigger(TriggerReceiver.java:73)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver.onReceive(AbstractTriggerReceiver.java:65)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.trigger(Notification.java:254)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.schedule(Notification.java:206)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Manager.schedule(Manager.java:100)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.localnotification.TriggerReceiver.onTrigger(TriggerReceiver.java:73)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver.onReceive(AbstractTriggerReceiver.java:65)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.trigger(Notification.java:254)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.schedule(Notification.java:206)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Manager.schedule(Manager.java:100)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.localnotification.TriggerReceiver.onTrigger(TriggerReceiver.java:73)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver.onReceive(AbstractTriggerReceiver.java:65)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.trigger(Notification.java:254)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.schedule(Notification.java:206)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Manager.schedule(Manager.java:100)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.localnotification.TriggerReceiver.onTrigger(TriggerReceiver.java:73)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver.onReceive(AbstractTriggerReceiver.java:65)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.trigger(Notification.java:254)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.schedule(Notification.java:206)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Manager.schedule(Manager.java:100)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.localnotification.TriggerReceiver.onTrigger(TriggerReceiver.java:73)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver.onReceive(AbstractTriggerReceiver.java:65)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.trigger(Notification.java:254)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.schedule(Notification.java:206)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Manager.schedule(Manager.java:100)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.localnotification.TriggerReceiver.onTrigger(TriggerReceiver.java:73)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver.onReceive(AbstractTriggerReceiver.java:65)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.trigger(Notification.java:254)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.schedule(Notification.java:206)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Manager.schedule(Manager.java:100)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.localnotification.TriggerReceiver.onTrigger(TriggerReceiver.java:73)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver.onReceive(AbstractTriggerReceiver.java:65)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.trigger(Notification.java:254)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.schedule(Notification.java:206)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Manager.schedule(Manager.java:100)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.localnotification.TriggerReceiver.onTrigger(TriggerReceiver.java:73)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver.onReceive(AbstractTriggerReceiver.java:65)
02-13 12:59:02.658  8854  8854 W System.err:    at de.appplant.cordova.plugin.notification.Notification.trigger(Notification.java:254)
...

Continues like this for several MB

alesgenova commented 6 years ago

Btw, I'm probably gonna use this workaround in the meantime, since it seems to be working somewhat ok.

setNotifications(every: string, firstAt: Date){
    let notifications = [];
    let n:number = 63; // arbitrary large number
    let title: string = "Title";
    let text: string = "Text";

    for (let i=0; i<n; i++){
      let theAt = new Date(firstAt);
      if (every == 'minute'){
        theAt.setMinutes(theAt.getMinutes()+i);
      }else if (every == 'hour'){
        theAt.setHours(theAt.getHours()+i);
      }else if (every == 'day'){
        theAt.setDate(theAt.getDate()+i);
      }else if (every == 'week'){
        theAt.setDate(theAt.getDate()+i*7);
      }
      notifications.push(
        {
          id: i,
          title: title,
          text: text,
          icon: "",
          sound: true,
          smallIcon: "res://"+this.notificationStrings.icon,
          //foreground: true,
          wakeup: false,
          trigger:{
            at: theAt
          }
        }
      );
    }
    cordova.plugins.notification.local.schedule(notifications,()=>{
      console.log("notification set ", firstAt, every);
    });
}

Every time the app is started, I cancelAll() and then reset a new set of notifications, making sure that firstAt is at the appropriate time in the future.

alesgenova commented 6 years ago

Nevermind, it looks like my install script reverted automatically to 0.9.0-beta2 when installing the plugin using @latest . 0.9.0-beta3 doesn't show the repetition behavior.

However, I will stick to using my workaround, because it should work on iOS too where firstAt is not available, and it also restores the notifications after a reboot on android if foreground: true and priority: 1 is set.

Thanks for the plugin!

alesgenova commented 6 years ago

Actually, I tried again today, and the problem seems to be there on beta3 as well. Reopening the issue

katzer commented 6 years ago

@alesgenova Please make sure again that you're running beta.3 and provide a working sample. In case of date issues its important to know how the start date and the trigger date correspond together. For example the endless loop happens by setting trigger.at to now.

alesgenova commented 6 years ago

Yes this time I'm sure I'm running beta.3

The code to reproduce it it's the following.

What I expect: I expect a notification to be fired every hour at :00 minutes. What happens: At the expected time, infinite notifications get triggered (like 2 or more per second), and they keep on firing until the clock turns to :01

let notifications = [
      {
        id: 0,
        title: "title",
        text: "text",
        icon: "",
        smallIcon: "res://"+this.notificationStrings.icon,
        wakeup: false,
        foreground: true,
        priority: 1,
        trigger: {
          every: {minute: 0},
        }
      }
]

cordova.plugins.notification.local.schedule(notifications,()=>{
      console.log("notification set ");
    });
katzer commented 6 years ago

@alesgenova Yes thats a bug in the code for Android and Windows if the minute is lower compared to the base date. However there's no infinite loop. The plugin wont schedule any notification because the calculated date is in the past.

renato-rodrigues-t1 commented 6 years ago

Just use : trigger: { every: { hour: 20, minute: 20, second: 15 }, count: 1 },

it will be fired only once at the right time.

Hope it works 4 u too!

chakravarthy426 commented 6 years ago

I am getting this error message, This is my code this.localNotifications.schedule({ id:1, title:"Reminder", text:"Please drink water", priority:2, trigger:{ every:{minute:30}}
}) trigger: { every: { minute: number; ...' is not assignable to parameter of type 'ILocalNotification | ILocalNotification[]' I want to get a notification on every 30 minutes.

Simpler1 commented 6 years ago

To avoid the repeated notifications every second, set "count" to some large number.

cordova.plugins.notification.local.schedule({
  id: 0,
  title: title,
  text: text,
  trigger: {
    firstAt: at,
    every: 'minute',
    count: 1440,
  },
});
devriescs commented 5 years ago

This seems to work:

import {LocalNotifications, ELocalNotificationTriggerUnit} from '@ionic-native/local-notifications';

` let date = new Date(new Date().getTime() + 3600);

this.localNotifications.schedule({ text: 'Local notification', trigger: { firstAt: date, every: ELocalNotificationTriggerUnit.MINUTE, count: 5, }, });`

Otziii commented 5 years ago

@katzer firstAt is not working on iOS, so how on earth is it possible to trigger a notification on a specific time and then let it trigger every minute from that time unless it's cancelled.

Not working: this.localNotifications.schedule({ id, text: title, trigger: { at: when.toDate() }, every: "minute", });

Not working: this.localNotifications.schedule({ id, text: title, trigger: { at: when.toDate(), every: { minute: 0 }, }, });

Not working: this.localNotifications.schedule({ id, text: title, trigger: { at: when.toDate(), every: ELocalNotificationTriggerUnit.MINUTE }, });

This works, but then it doesn't fire every minute... When using 'at' and 'every' together, the notification does not get scheduled at all. this.localNotifications.schedule({ id, text: title, trigger: { at: when.toDate() }, });

marioshtika commented 5 years ago

Hello everybody!

I had this problem for a long time and I just found out that the master branch has already resolved this issue. All this time I installed it through package.json so I was using the "0.9.0-beta.3" and "0.9.0-beta.2"

So if you use the repository url in the config.xml file, it will work. Or you can fork the project and make your own release.

@katzer can you please make a release with your latest commits.

Thank you in advance.

l364cyv1 commented 5 years ago

@marioshtika I have the same issue. Are you saying that in 0.9.0-beta.3 this issue does not exist and npm version is 0.9.0-beta.2?

marioshtika commented 5 years ago

Hello @stefaneli31, watch the below timeline to understand what I am saying.

11 Jan 2018: 0ac0372 - Release v0.9.0-beta.2 (Issue exists) 13 Jan 2018: a75c097 - Release 0.9-beta.3 (Issue exists) 21 Feb 2018: b93cf65 - Fix bug in match trigger where interval is every minute (Issue fixed) . . . 7 Feb 2019: 06baff7 - Fix wrong prio constants (latest commit with a lot of other bug fixed)

Right now the npm version is 0.9.0-beta.2. But in both '0.9.0-beta.2' and '0.9.0-beta.3' this issue still exists. Only if you download the master branch you will not have this problem.

Or the best solution would be if @katzer could make a new release with all the new commits.

l364cyv1 commented 5 years ago

@marioshtika Hi, thank you for the explanation. It works. :) Also just to point out you can download the master branch by specifying the branch after hashtag - like this: spec="https://github.com/katzer/cordova-plugin-local-notifications.git#master"

marioshtika commented 5 years ago

@stefaneli31 You are right. I think it also needs the 'ANDROID_SUPPORT_V4_VERSION'

on config.xml

<plugin name="cordova-plugin-local-notification" spec="https://github.com/katzer/cordova-plugin-local-notifications.git#master">
    <variable name="ANDROID_SUPPORT_V4_VERSION" value="26.+" />
</plugin>
MoradAbdelgaber commented 5 years ago

@stefaneli31 I am using this version , but the plugin only trigger the last notifications l scheduled . ??

marioshtika commented 5 years ago

@MoradAbdelgaber do you have a different id for each notification? example:

cordova.plugins.notification.local.schedule([
    { id: 1, title: 'My first notification' },
    { id: 2, title: 'Another notification' }
]);
MoradAbdelgaber commented 5 years ago

@marioshtika. Yes I provide differrent Id , I am using Math.random() functionality to handle this . Any idea to solve this ?

marioshtika commented 5 years ago

Do you mean Math.random() functionality? Can you provide some code sample?

MoradAbdelgaber commented 5 years ago

@marioshtika Let rand = Math.random(); Cordova.plugins.notification.local.schedule({ id : rand , title : 'title' , text : 'text', foreground : true , trigger : { every : { weekday : 1 , hour : 9 , minute : 0

} } })

marioshtika commented 5 years ago

I think the Math.random() gives you a random number from 0 to 1.

Another approach is the below

lastNotificationId: number;

constructor() {
  this.localNotifications.getIds()
    .then((ids: number[]) => {
      this.lastNotificationId = ids.length > 0 ? Math.max(...ids) : 0;
    })
}

schedule() {
  this.lastNotificationId++;
  Cordova.plugins.notification.local.schedule({
    id: this.lastNotificationId,
    .
    .
    .
  });
}

console.log your notification for debugging and see what id did you use.

MoradAbdelgaber commented 5 years ago

I have solved the problem . The problem was the plugin see all the ids is the same if it is not equal mathematically. I have changed the id from Math.random() to new Date().getTime().

codeboros commented 5 years ago

i installed the latest head version, works fine right now. cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications.git

Kudla161992 commented 5 years ago

@codeboros @marioshtika is the plugin working fine ? I am still facing the issue when i try to trigger the notification for every week in android 9. Can you please help me in solving this? Which is the stable version i need to install ?

marioshtika commented 5 years ago

@Kudla161992 are you still having problem with the countless notifications?

the solution is adding the plugin using its repository url: cordova plugin add https://github.com/katzer/cordova-plugin-local-notifications.git#master

make sure you remove any previous version of this plugin before adding the new one.

check if the plugin exists with: cordova plugin list

and remove previous version with: cordova plugin add cordova-plugin-local-notifications

Kudla161992 commented 5 years ago

@marioshtika I followed the same steps as per your comment. Now the problem of countless trigger is solved but unfortunately , the notification doesn't trigger for the next scheduled time. Suppose I schedule the notification for all the days of the week, it triggers only once for today and no notifications are triggered for the other scheduled days.

Package.json: "cordova-plugin-local-notification": "git+https://github.com/katzer/cordova-plugin-local- notifications.git#master" "cordova-plugin-local-notification": { "ANDROID_SUPPORT_V4_VERSION": "26.+" }

Code:

this.localNotifications.schedule({ id:id, title: title, text: 'My Notification', trigger: { every : { weekday: Date.getDay(), hour:Date.getHours() , minute:Date.getMinutes() }}, sound: 'res://platform_default',
data: { day: reminderItem.day } }); Please check the trigger part of the code. Not sure if i am doing something wrong here. I want the notification to trigger everyday(or the day selected by the user )on the scheduled time.

marioshtika commented 5 years ago

Hello @Kudla161992. How are you testing it. I think the above code is triggered every week not every day. Example

every : {
    weekday: 1,
    hour: 10,
    minute:0
}

This will notify you every Monday at 10 o'clock

Try testing it by scheduling every minute, just to test if it is working.

Pratikshakhandagale commented 4 years ago

Just use : trigger: { every: { hour: 20, minute: 20, second: 15 }, count: 1 },

it will be fired only once at the right time.

Hope it works 4 u too!

@renato-rodrigues-t1 Is it fired every day at mentioned time ( { hour: 20, minute: 20, second: 15 }) ???

JyotirmayaV commented 4 years ago

Hi , I have recently downloaded the plugin using

cordova plugin add cordova-plugin-local-notification

and facing the same error of the app becoming unresponsive for the time of 1 minute in which notification was scheduled using early. I used count : 1 but when I used that notification was shown for only once and with no repeat for consecutive days. this.localNotifications.schedule( { id : new Date().getTime(), title: this.name + " Hello", text: ''Please guide", trigger: { every : { hour : Number(hours),minute : Number(minutes) } , count : 1 }, } );

codewise-nicolas commented 3 years ago

Is this the same issue that is dealt with here https://stackoverflow.com/questions/55379397/local-notification-trigger-continuously-for-one-minute-in-android-device/56747645#56747645 ? (That fix to the java file fixed the repeating notification on android for our project)