Search code examples
androidreact-nativefirebase-cloud-messagingreact-native-firebase

React native firebase multiple notification triggered in background in one event


The background notification is triggered two times in one event, which should not happen, I am unable to detect the issue? I am using react native firebase with notifee for notification..! This is the useEffect code, where i unsubscribe the foreground notification listener.

useEffect(() => {
    networkCall()
    fetchingUserData().then(() => {
        setIsLoading(false)
    })

    //
    // This is a function for receiving the notification message and displaying notification
    //
    async function onMessageReceivedFore(message: any) {
        onDisplayNotification(message);
        console.log('message received Foreground');
    }
    async function onMessageReceivedBack(message: any) {
        onDisplayNotification(message);
        console.log('message received Background');
    }
    //
    //This is for receiving notification onForeground
    //
    const unsubscribe = messaging().onMessage(onMessageReceivedFore);
    //
    //This is for receiving notification on background
    //
    messaging().setBackgroundMessageHandler(onMessageReceivedBack);

    return () => {
        console.log('component unmount');
        onNotificationInteraction()
        unsubscribe()
    }

}, [])

Solution

  • I was using both onForeground() notification and onBackground() notification, when I removed the onBackground() notification then , the onForeground method was also triggered in the background so, I was receiving two notifications in one event... This shouldn't be the case but, seems to work now...

    useEffect(() => {
        networkCall()
        fetchingUserData().then(() => {
            setIsLoading(false)
        })
    
        //
        // This is a function for receiving the notification message and displaying notification
        //
        async function onMessageReceivedFore(message: any) {
            onDisplayNotification(message);
            console.log('message received Foreground');
        }
        //
        //This is for receiving notification onForeground
        //
        const unsubscribe = messaging().onMessage(onMessageReceivedFore);
    
        return () => {
            console.log('component unmount');
            onNotificationInteraction()
            unsubscribe()
        }
    
    }, [])