React-native-fcm: FCM.presentLocalNotification in componentDidMount called infinitely

Created on 15 Oct 2016  路  4Comments  路  Source: evollu/react-native-fcm

"react": "15.3.2", "react-native": "0.34.1", "react-native-fcm": "^2.3.2",

componentDidMount() { FCM.getFCMToken().then(token => { console.log(FCM,token) // store fcm token in your server }); this.notificationUnsubscribe = FCM.on('notification', (notif) => { console.log('notification', FCM, notif); if(notif.local_notification){ //this is a local notification console.log('local_notification') } if(notif.opened_from_tray){ //app is open/resumed because user clicked banner console.log('opened_from_tray') } console.log('-------------------------------------') FCM.presentLocalNotification({ body: 'New message, check it out!', priority: 'high', title: 'Chat', sound: 'default', show_in_foreground: true, tag:'CHAT' }); }); }

Now Once the app is in foreground and if i trigger notification from firbase console, there are infinite local notification i.e FCM.on('notification') is called infinitely.
@evollu Need Help.

Most helpful comment

That's because your listener also listens to the local_notification that you just fired.

if (!notif.local_notification) {
  FCM.presentLocalNotification({...})
}

All 4 comments

Did you find any solution to your problem?

i don't know the exact solution but found a work around that is working for me. i had send a key in payload and presentLocalNotification is triggered only when i have notif.key === 'which i send in payload'

Hope it help.

That's because your listener also listens to the local_notification that you just fired.

if (!notif.local_notification) {
  FCM.presentLocalNotification({...})
}

Hi @woniesong92 , unfortunately I am facing the same issue but the solution didn't work for me.

My code:

        FCM.requestPermissions()
            .then(() => console.log("granted"))
            .catch(() => console.log("notification permission rejected"))

        FCM.getFCMToken().then(token => {
            console.log("  >>token:", token)
            // store fcm token in your server
            log2s(`FCM.getFCMToken: ${token}`)
            window.fcmId = token
            saveAppLocation()

            // FCMEvent.Notification.displayNotification('Hey.. hi')
        })

**FCM.on(FCMEvent.Notification, notif => {
            // if (notif.fcm && notif.fcm.body) {
            /* Create local notification for showing in a foreground */

            if (notif.opened_from_tray) {
                if (!notif.local_notification) {
                    FCM.presentLocalNotification({
                        body: 'j j okocha',
                        priority: "high",
                        title: 'legend',
                        sound: "default",
                        show_in_foreground: true,
                        "large_icon": "ic_launcher",// Android only
                        icon: "ic_launcher",
                        "show_in_foreground": true, /* notification when app is in foreground (local & remote)*/
                        vibrate: 300, /* Android only default: 300, no vibration if you pass null*/
                        "lights": true, // Android only, LED blinking (default false)
                        status: "23"
                    });
                }
            }
        })**

I receive notification in background ..
If i remove condition if (notif.opened_from_tray) {, then I also get notification when app is open/is in foreground. But, when in foreground, I get infinite messages.

Please help !!! Thanks...

Was this page helpful?
0 / 5 - 0 ratings