Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: OneSignal.Notifications.addEventListener('click'.....) - event is fires dozens of times when app starts. #970

Open
2 of 3 tasks
kpturner opened this issue Feb 1, 2024 · 6 comments

Comments

@kpturner
Copy link

kpturner commented Feb 1, 2024

What happened?

I hope this is a symptom of a development environment only, but when I define the listener below the click event fires dozens of times when the app starts up without any notification actually being clicked. This us a vue application using nuxt and so has a HMR function for development. It is possible that the listener is being added over and over again - but it doesn't explain why it fires over and over again (providing the exact same event every time).

  OneSignal.Notifications.addEventListener('click', async (event) => {
    logger.info('Push notification clicked:', event)
  })

Steps to reproduce?

Described above

What did you expect to happen?

The event should only fire if a notification is clicked.

OneSignal Cordova SDK version

5.0.4

Which platform(s) are affected?

  • iOS
  • Android

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@nan-li
Copy link
Contributor

nan-li commented Feb 1, 2024

Hi @kpturner,

It may be related to the HMR so your addEventListener is actually getting called multiple times. You can add some kind of log before you call addEventListener to confirm. The SDK supports having multiple listeners for each event.

However, they shouldn't fire if no notification is clicked. What exactly prints in the logger.info('Push notification clicked:', event)?

You can also try removing the async, I am not sure if that will have any effect.

@kpturner
Copy link
Author

kpturner commented Feb 1, 2024

The async isn't relevant I'm afraid. The function awaits an async function after receiving the event. I removed it in the example but forgot to remove the async on the definition. But it wouldn't make any difference here.

The event received is actually a real event from a clicked notification - but it was only clicked once much earlier today.

I redeployed the app on the emulator which broke the cycle. As I said I can see why it's adding multiple listeners but I am not sure why it's firing the event multiple times 🤷‍♀️ it

@nan-li
Copy link
Contributor

nan-li commented Feb 1, 2024

As I said I can see why it's adding multiple listeners but I am not sure why it's firing the event multiple times 🤷‍♀️ it

Each time the anonymous listener is added, it is added to a queue of listeners.

When the click event is processed, it fires all the listeners with the event. If you add this listener 5 times, all 5 will trigger with the same information.

@kpturner
Copy link
Author

kpturner commented Feb 1, 2024

But why would it fire the click event at all if no notification has been clicked? It's as if the event is stored somewhere and every time there is an HMR something decides to fire the event again.

@selected-pixel-jameson
Copy link

Confirming that this only happens when HMR is in affect.

@anatoly-spb
Copy link

Confirm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants