Cordova-plugin-local-notifications: on ('click) ran for multiple times

Created on 22 Jun 2017  路  8Comments  路  Source: katzer/cordova-plugin-local-notifications

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:3.12.1
  • Platform: android
  • OS version:
  • Device manufacturer / model: redmi note 3
  • Cordova version (cordova -v):7.0.1
  • Cordova platform version (cordova platform ls): android 6.2.3
  • Plugin config
  • Ionic Version (if using Ionic) 3.4.0

Expected Behavior

this.localNotifications.on("click", function(notification){ console.log("sup"); });

the "sup" should appear only one time.

Actual Behavior

the "sup" appear more than one time

Steps to Reproduce

_Reproduce this issue; include code to reproduce, if relevant_

setReminder(){ let options = { id: this.info.id, title: this.info.title, text: this.info.message, at: new Date(this.info.enddate), sound: 'file://audio/song.wav', }; this.localNotifications.schedule(options);
}
constructor(){ this.localNotifications.on("click", function(notification){ console.log("sup"); });
}

Context

i will first schedule the notification.

After the notification appear, when the user click on the notification it will update the database when the user click on the notification.

Debug logs

_Include iOS / Android logs_

  • ios XCode logs
  • Android: $ adb logcat
android Ionic 2

Most helpful comment

I was facing the same issue. However, I later realized the mistake I was making:
I had the registration code for "click" event in the same function as that for handling a new notification. This way, every time a new notification came, the event registration also increased by 1 and would lead to multiple firings of the event:
receiveNotificationMessage(message) { console.log("Notification Received: " + message); let notificationData = JSON.parse(message); this.localNotifications.schedule({ id: notificationData.Id, title: "Notification Recieved", text: "Notification Body", led: 'FF0000', autoClear: true, smallIcon: 'res://notification.png', data: notificationData }); let onclickObservable = this.localNotifications.on("click"); onclickObservable.subscribe((notification) => { console.log(notification); });
I solved this by moving the "click" registration to the constructor.
Try to confirm that your constructor is not being fired multiple times.

All 8 comments

How are you triggering the event? Thats what the "Steps to Reproduce" is for.

sorry for missed out, i have updated the steps to reproduce.

So is the notification in the background, the foreground, is the app stopped or started?

it is on "background", and the app did start, works like normal. the things is tat it trigger the console.log multiple times instead of one.

I'll check it again with Android 7.0 but with Ionic 1. My recollection was that we never got ANY "click" event to work. We don't use Ionic 2 so can't comment on that.

I have the same problem, did you find a work around yet?

Please try out with 0.9-beta

If the callback gets called multiple times, its often that the code adds the callback within a callback, which was executed twice.

I was facing the same issue. However, I later realized the mistake I was making:
I had the registration code for "click" event in the same function as that for handling a new notification. This way, every time a new notification came, the event registration also increased by 1 and would lead to multiple firings of the event:
receiveNotificationMessage(message) { console.log("Notification Received: " + message); let notificationData = JSON.parse(message); this.localNotifications.schedule({ id: notificationData.Id, title: "Notification Recieved", text: "Notification Body", led: 'FF0000', autoClear: true, smallIcon: 'res://notification.png', data: notificationData }); let onclickObservable = this.localNotifications.on("click"); onclickObservable.subscribe((notification) => { console.log(notification); });
I solved this by moving the "click" registration to the constructor.
Try to confirm that your constructor is not being fired multiple times.

Was this page helpful?
0 / 5 - 0 ratings