OneSignal / react-native-onesignal

React Native Library for OneSignal Push Notifications Service
Other
1.57k stars 374 forks source link

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

Open akeva001 opened 1 year ago

akeva001 commented 1 year ago

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

jennantilla commented 1 year ago

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

jackculprit commented 1 year ago

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

kpturner commented 10 months ago

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 commented 9 months ago

You can call

OneSignal.Notifications.clearAll()

before adding event listener to clear all previous listeners

kpturner commented 9 months ago

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 commented 9 months ago

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 commented 3 months ago

This problem still persists. Has anyone solved it?

AbdulBasit-Kagzi commented 2 months ago

` 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

falihnaufal17 commented 3 days ago

i stil fight with this problem :) can anyone tell how to solve this? my app always listen event click when open the app or reload the app

kpturner commented 2 days ago

i stil fight with this problem :) can anyone tell how to solve this? my app always listen event click when open the app or reload the app

I doubt you will see the problem in production. It is usually just a symptom of the dev environment - especially if you use HMR

falihnaufal17 commented 2 days ago

i stil fight with this problem :) can anyone tell how to solve this? my app always listen event click when open the app or reload the app

I doubt you will see the problem in production. It is usually just a symptom of the dev environment - especially if you use HMR

So if i try to build with APK release mode or AAB, the above problem will not occur right?