Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

messaging().onNotificationOpenedApp calling multiple times

when i click on a notification , messaging().onNotificationOpenedApp function calling multiple times

useEffect(() => {
messaging().onNotificationOpenedApp(async remoteMessage => {
  if (remoteMessage) {
    const notification = 
      JSON.parse(String(remoteMessage.data));
      const notificationData = NotificationMapper([notification]);
      await notificationNavigation(notificationData[0]);
  }
})}, []);

this is my code ,please help me out to prevent calling above function multiple times

like image 822
MURARI Yadav Avatar asked Nov 28 '25 17:11

MURARI Yadav


1 Answers

you're missing the listener remover when your component gets unmounted

useEffect(() => {
  // assign the listener to a variable
  const listener = messaging().onNotificationOpenedApp(async remoteMessage => {
    if (remoteMessage) {
      const notification = JSON.parse(String(remoteMessage.data));
      const notificationData = NotificationMapper([notification]);
      await notificationNavigation(notificationData[0]);
    }
  })

  // and remove it when your component gets unmounted
  return () => listener()
}, []);
like image 99
luicfrr Avatar answered Dec 02 '25 03:12

luicfrr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!