React-native-firebase: Loop inifine when createLocalNotification call on firebase.messaging().onMessage

Created on 2 Nov 2017  路  7Comments  路  Source: invertase/react-native-firebase

Issue

Environment

  1. Application Target Platform: Android

  1. Development Operating System: Windows 7

  1. Build Tools: Android SDK 23.0.1

  1. React Native version: 0.48.1

  1. RNFirebase Version: 3.0.5

  1. Firebase Module: Cloud Messaging
firebase.messaging().onMessage((message) => {
    //console.log(message);

    if(message.isTrip === '1'){
        Alert.alert( message.title ? message.title : 'Boss Attendance System', 
         message.body ? message.body: 'Start trip', 
        [   
            {
                text: 'Ignore', 
                onPress: () => console.log('Cancel Pressed'), style: 'cancel'}, 
            {
                text: 'Show', 
                onPress: () => this.props.navigation.navigate("Trip")}, 
        ], { cancelable: false } )  
    }
    else{
        firebase.messaging().createLocalNotification({
           title: 'My app',     
           body: 'Hello world',
           icon: 'ic_launcher',
           priority: 'high',
           click_action: 'ACTION',
           show_in_foreground: true,
           sound: 'default',
           opened_from_tray: true,
           big_text: 'i am large, i am large'
        })
        //console.log(message);
        //this.showMessagging();

        // /global.commonAlert(message.body ? message.body: 'Start trip', message.title ? message.title : 'Boss Attendance System');
    }           
});

Android debug logging log.txt

User Issue

Most helpful comment

@numez if app is in background it will show double notification, use this way:

      // v3.x
      if (AppState.currentState === 'active') {
        firebase.messaging().createLocalNotification({
          ...notif,
          local_notification: true,
          show_in_foreground: true
        })
      }

     // v4.x 
    firebase.notifications().onNotification(async notif => {

        // create local notification
        let local = new firebase.notifications.Notification()
        .setNotificationId('main')
        .setTitle(notif._title)
        .setBody(notif._body)
        .setSound('default')

        if (Platform.OS === 'android') {
            local
                .android.setChannelId('main')
                .android.setAutoCancel(true)
                .android.setDefaults(firebase.notifications.Android.Defaults.All)
                .android.setBadgeIconType(firebase.notifications.Android.BadgeIconType.Small)
                .android.setCategory(firebase.notifications.Android.Category.Recommendation)
        }

        firebase.notifications().displayNotification(local)
    }

All 7 comments

@sirstevensu Your createLocalNotification() call will trigger onMessage which will in turn trigger another createLocalNotification() call, hence the infinite loop.

This is code that you need to fix - you have to protect against this by looking at the content of the message and detecting whether it should trigger a local notification

@chrisbianca is createLocalNotification part of the v3.1?

I'm not sure as it's not listed in the documentation and I'm aware you are considering moving this out of the main react-native-firebase library.

@grifotv We haven't made any changes to messaging in v3.0 or v3.1 so anything that worked on v2 will still do so.

This is going to be our primary focus for v3.2. We haven't decided exactly how we're going to proceed with this, but we'll make sure it's fully documented once we've decided!

@chrisbianca thanks for the quick response!

I am experiencing the samme issue, however, I am not creating new notifications within onMessage() callback.

What I am doing is opening a uri with react-native Linking API. Linking.openURL('marketplace://marketplace/tabs/messages/id')

This link is then handled by the apps uri scheme and handed over to React-Navigation which handles the url. The consecutive notifications that are handled are missing all fields except opened_from_tray: true and finish().

@sirstevensu to prevent infinite loop just add a property to message

firebase.messaging().onMessage((message)=>{
  if(message.local_notification) return;

  firebase.messaging().createLocalNotification({
    ...message.fcm,
    local_notification: true, // prevent loop
    show_in_foreground: true,
    priority: "high",
  });
});

@numez if app is in background it will show double notification, use this way:

      // v3.x
      if (AppState.currentState === 'active') {
        firebase.messaging().createLocalNotification({
          ...notif,
          local_notification: true,
          show_in_foreground: true
        })
      }

     // v4.x 
    firebase.notifications().onNotification(async notif => {

        // create local notification
        let local = new firebase.notifications.Notification()
        .setNotificationId('main')
        .setTitle(notif._title)
        .setBody(notif._body)
        .setSound('default')

        if (Platform.OS === 'android') {
            local
                .android.setChannelId('main')
                .android.setAutoCancel(true)
                .android.setDefaults(firebase.notifications.Android.Defaults.All)
                .android.setBadgeIconType(firebase.notifications.Android.BadgeIconType.Small)
                .android.setCategory(firebase.notifications.Android.Category.Recommendation)
        }

        firebase.notifications().displayNotification(local)
    }

Was this page helpful?
0 / 5 - 0 ratings