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

[question]: Why is the notifications event listener (both click and foregroundWillDisplay) event firing more than once? #1548

Open
1 task done
akeva001 opened this issue Sep 2, 2023 · 8 comments

Comments

@akeva001
Copy link

akeva001 commented Sep 2, 2023

How can we help?

I am using the event listeners as described in the setup guide, but when I receive a notification, the events fire more than 10 times. How can I fix this?

Code of Conduct

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

Hello @akeva001 would you be able to send the logs from your device reproducing this behavior? We'll be happy to investigate. Thanks!

@jackculprit
Copy link

I have the same issue. Listeners are not getting removed on reload when developing locally.

@kpturner
Copy link

kpturner commented Feb 1, 2024

I stumbled across this log because I have just realised that in my own development environment the click event defined below is literally firing 45 (and growing) times when the application starts.......and no notifications have been clicked at all! :(

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

Seems consistent with HMR in play.

@romanesko
Copy link

You can call

OneSignal.Notifications.clearAll()

before adding event listener to clear all previous listeners

@kpturner
Copy link

You can call

OneSignal.Notifications.clearAll()

before adding event listener to clear all previous listeners

Is there documentation regarding that method that states that it will remove existing listeners? The SDK just says Removes all OneSignal notifications. which is pretty ambiguous to say the least.

@romanesko
Copy link

The SDK just says Removes all OneSignal notifications. which is pretty ambiguous to say the least.

You are right. It's about removing notifications.

@ByDodo
Copy link

ByDodo commented Aug 9, 2024

This problem still persists. Has anyone solved it?

@AbdulBasit-Kagzi
Copy link

AbdulBasit-Kagzi commented Sep 5, 2024

` useEffect(() => {
const onForegroundWillDisplay = async (event: any) => {
event.preventDefault();
console.log('OneSignal: notification will be displayed', event);

  // Perform async work here
  const result = await dispatch(
    GetNotificationThunk({userId: user.id as string}),
  );

  // Display the notification after async work
  event.getNotification().display();
};

// Add the event listener
OneSignal.Notifications.addEventListener(
  'foregroundWillDisplay',
  onForegroundWillDisplay,
);

// Cleanup function to remove the event listener on unmount or before re-render
return () => {
  OneSignal.Notifications.removeEventListener(
    'foregroundWillDisplay',
    onForegroundWillDisplay,
  );
};

}, [dispatch]); // Add dispatch to dependency array if needed`

I am facing the same problem eventListener is getting called multiple. times

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

7 participants