cordova-plugin-local-notifications icon indicating copy to clipboard operation
cordova-plugin-local-notifications copied to clipboard

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

Open alesgenova opened this issue 7 years ago • 31 comments

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

  • Plugin version: 0.9-beta.3
  • Platform: Android
  • OS version: 8.1 and 7.1
  • Device manufacturer / model: Nexus 5X
  • Cordova version (cordova -v): 8.0.0
  • Cordova platform version (cordova platform ls): android 6.4.0
  • Plugin config
  • Ionic Version (if using Ionic) 3.9.2, but using the plugin directly, without the ionic-native layer

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 avatar Feb 13 '18 18:02 alesgenova

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 avatar Feb 13 '18 19:02 alesgenova

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 avatar Feb 13 '18 20:02 alesgenova

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

alesgenova avatar Feb 17 '18 20:02 alesgenova

@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.

katzer avatar Feb 20 '18 11:02 katzer

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 ");
    });

alesgenova avatar Feb 20 '18 15:02 alesgenova

@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.

katzer avatar Feb 21 '18 10:02 katzer

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 avatar Mar 12 '18 23:03 renato-rodrigues-t1

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.

chakravarthy426 avatar Jul 31 '18 05:07 chakravarthy426

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,
  },
});

Simpler1 avatar Sep 06 '18 14:09 Simpler1

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, }, });`

devriescs avatar Oct 11 '18 07:10 devriescs

@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() }, });

Otziii avatar Dec 12 '18 19:12 Otziii

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.

marioshtika avatar Feb 14 '19 13:02 marioshtika

@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?

l364cyv1 avatar Feb 21 '19 02:02 l364cyv1

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.

marioshtika avatar Feb 21 '19 07:02 marioshtika

@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"

l364cyv1 avatar Feb 21 '19 09:02 l364cyv1

@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>

marioshtika avatar Feb 21 '19 11:02 marioshtika

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

MoradAbdelgaber avatar Mar 06 '19 21:03 MoradAbdelgaber

@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' }
]);

marioshtika avatar Mar 12 '19 07:03 marioshtika

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

MoradAbdelgaber avatar Mar 12 '19 08:03 MoradAbdelgaber

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

marioshtika avatar Mar 12 '19 08:03 marioshtika

@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

} } })

MoradAbdelgaber avatar Mar 12 '19 09:03 MoradAbdelgaber

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.

marioshtika avatar Mar 12 '19 11:03 marioshtika

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().

MoradAbdelgaber avatar Mar 12 '19 11:03 MoradAbdelgaber

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

codeboros avatar Jul 10 '19 20:07 codeboros

@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 ?

Kudla161992 avatar Jul 24 '19 06:07 Kudla161992

@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

marioshtika avatar Jul 24 '19 07:07 marioshtika

@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.

Kudla161992 avatar Jul 24 '19 10:07 Kudla161992

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.

marioshtika avatar Jul 24 '19 10:07 marioshtika

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 }) ???

Pratikshakhandagale avatar Nov 19 '19 06:11 Pratikshakhandagale

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 }, } );

JyotirmayaV avatar Sep 11 '20 17:09 JyotirmayaV

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)

codewise-nicolas avatar Oct 01 '21 07:10 codewise-nicolas