evollu / react-native-fcm

react native module for firebase cloud messaging and local notification
MIT License
1.73k stars 681 forks source link

FCM.on(FCMEvent.Notification) triggered twice for every notification #471

Closed connercms closed 6 years ago

connercms commented 7 years ago

@evollu We are using fcm-django library to construct a push notifications. The following is our code to generate our notif from the django app

...send_message(
    'type': 'dispatch_notification',
    'record_id': dispatch_id,
    'call_back_number': call_back_number,   
    'custom_notification': {
        'body': 'New Dispatch Available',
        'title': 'Dispatch created',
        'priority': 'high',
        'id': generate_short_uuid(),
        'click_action': 'fcm.ACTION.HELLO',
        'content_available': True,
        'show_in_foreground': True,
    },
)                

And the code on our react-native side

class PushNotificationsController extends Component {
  componentWillMount() {
    // Request permissions for ios
    FCM.requestPermissions()

    FCM.getFCMToken().then(token => {
      this.props.storeFCMToken(token)
    })

    FCM.getInitialNotification().then(notif => {
      console.log("Initial Notification", notif)
    })

    this.notificationListener = FCM.on(FCMEvent.Notification, notif => {
      // TODO: Called twice for every notif
      if (notif.local_notification){
        console.log('local notification')
        return
      }
      if (notif.opened_from_tray) {
        console.log('opened from tray')
        return
      }
    })

    this.refreshTokenListener = FCM.on(FCMEvent.RefreshToken, (token) => {
      this.props.storeFCMToken(token)
    })
  }

  componentWillUnmount() {
    this.notificationListener.remove()
    this.refreshTokenListener.remove()
  }

  render() {
    return null
  }
}

First notification payload looks like

{
    body: "New Dispatch Available"
    click_action: "fcm.ACTION.HELLO"
    content_available: true
    >finish: function finish(result)
    id: 584033945518084
    priority: "high"
    show_in_foreground: true
    title: "Dispatch Created"
}

Second notification payload looks like

{
    call_back_number: "5555555555"
    collapse_key: null
    custom_notification: "{"content_available":true,"show_in_foreground":true,"id":584033945518084,"body":"New Dispatch Available ","click_action":"fcm.ACTION.HELLO","priority":"high","title":"Dispatch Created"}"
    fcm: only contains proto
    finish: function finish(result)
    from: "3437766006454"
    google.message_id: "0:1500556198575562%7034eb85f9fd7ecd"
    google.sent_time: 1500556198569
    record_id: "115005561986855127"
    type: "dispatch_notification:
}

We are receiving notifications when app is in foreground, background, and killed, but FCM.on(FCMEvent.Notification) listener is called twice each time.

What version of RN and react-native-fcm are you running? RN: 0.46.2 RNFCM: 7.1.0 What device are you using? (e.g iOS9 emulator, Android 6 device)? Android LG V20 running Nougat (7.0) Is your app running in foreground, background or not running? Happens for each scenario

Also another issue we are running in to is the listener is triggered when the app is opened by pressing the app icon from the home screen of the device and opened_from_tray is always = 1, but docs say this should only happen when pressing banner?

evollu commented 7 years ago

for your first question. Can you add breakpoint at MessageService.java -> onMessageReceived and see how many times it gets called? This function should be only called once.

second question: for android there will be such noise. filter it out by some type attribute.

connercms commented 7 years ago

onMessageReceived is called only once.

evollu commented 7 years ago

ok I find the issue. I assume 2nd notification is the one you want? does 1st notification contain custom_notification

connercms commented 7 years ago

The second notification formatting seems strange. Everything in custom_notification is a string. Is it supposed to be this way? But yes, we would prefer the second notification as it has the data we are sending over

evollu commented 7 years ago

try master branch. it should only have the 2nd notif now. custom_notification is a string because firebase only supports 1 level deep object. it stringifies deeper object

connercms commented 7 years ago

That seems to be working. Only receiving the one notification now. However, when the app is not running and a notification comes in, and I press on the notification, FCM.getInitialNotification payload looks like first one mentioned above (without the data we send). Is there a way to retrieve this data?

evollu commented 7 years ago

that is expected. because custom_notification is actually building a local notification. you can have your properties inside custom_notification obj.

Jaafar-abusair commented 7 years ago

Probably you have another third part do implementing receive notification event

ghost commented 6 years ago

I have the same problem. MessageService.java -> onMessageReceived is triggered multiple times. How to solve this?

evollu commented 6 years ago

@ram-vertace it is more likely configuration issue. e.g. you have registered event multiple times. closing this as original issue is inactive

Kunalpaul12 commented 6 years ago

Please check settings.gradle file My issues:- I was calling this twice
include ':react-native-fcm' project(':react-native-fcm').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fcm/android')

check this ones