2

I use fcm-package (https://github.com/evollu/react-native-fcm) to send Firebase-Cloud-Messages to android.

Messages arrive at the phone.

If the app is in Background, the notification appears in the Statusbar. If I tap at the message, the App opens on main-screen - which is News_Screen in my case.

BUT I don't found a way to catch data of the notifications, after the App come to foreground.

FCM.on(FCMEvent.Notification, (notif) => { don't get any event.

If the App is in foreground, and I send a Notification, the console.log-output work.

Do I miss something special to be able to get event-data if app comes from background to foreground?

This is the structure of my app:

index.js ⇒ /app/index.js ⇒ (imports a StackNavigator)

Default Screen of StackNavigator is "News_Screen".

In News-Screen I have this:

async componentDidMount() {

        // START Firebase Cloud Messaging
        try {
            let result = await FCM.requestPermissions({
                badge: false,
                sound: true,
                alert: true
            });
        } catch (e) {
            console.error(e);
        }

        FCM.getFCMToken().then(token => {
            this.setState({token: token || ""});
            console.log("First the token: ", token)
            // store fcm token in your server
        });
        if (Platform.OS === "ios") {
            FCM.getAPNSToken().then(token => {
                console.log("APNS TOKEN (getFCMToken)", token);
            });
        }

FCM.on(FCMEvent.Notification, (notif) => {
            if(notif.opened_from_tray){
                setTimeout(()=>{
                    console.log("notif.opened_from_tray", notif);
                }, 1000)
            }

            // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
            console.log("Message erhalten", notif);
            if (notif.screen !== undefined) {
                console.log("Jo, der Screen is", notif.screen, this.props.navigation);
                this.props.navigation.navigate('Menu_Screen');
            }
        });
        FCM.on(FCMEvent.RefreshToken, (token) => {
            console.log("Again the token:", token)
            // fcm token may not be available on first load, catch it here
        });
    }

The only console.log I got if I push the notification and app comes from background to foreground is the First the token... Message.

How can I catch the Data of the notification. (E.g. if the Notification has as additional Object-Parmete: screen: 'Menu_Screen, to switch to this screen after App get to foreground)

suther
  • 12,600
  • 4
  • 62
  • 99

1 Answers1

0

change your listener to something like this..

FCM.on(FCMEvent.Notification, notif => {
      console.log("Notification", notif);

      if(Platform.OS ==='ios' && notif._notificationType === NotificationType.WillPresent && !notif.local_notification){
        notif.finish(WillPresentNotificationResult.All)
        return;
      }

      if(Platform.OS ==='ios'){
        switch(notif._notificationType){
          case NotificationType.Remote:
            notif.finish(RemoteNotificationResult.NewData)
            break;
          case NotificationType.NotificationResponse:
            notif.finish();
            break;
          case NotificationType.WillPresent:
            notif.finish(WillPresentNotificationResult.All)
            break;
        }
      }

      this.showLocalNotification(notif)
    });

showLocalNotification(notif) {
  console.log('here local', notif)
    FCM.presentLocalNotification({
      title: notif.title,
      body: notif.body,
      priority: "high",
      show_in_foreground: true,
      local: true
   });
}

if the app is foreground, this line NotificationType.WillPresent will catch the notification data.

Hazim Ali
  • 1,077
  • 4
  • 17
  • 28
  • Thank you for the hint, but I'm currently on Android and try to figure out, how the Push-Events are catched the right way. Do you know any Documentation for FCM, or how `onMessageReceived` in FCM work? `presentLocalNotification` seems not to work on Android?! – suther May 19 '18 at 16:46
  • @suther this.showLocalNotification(notif) supposed to call on android as well.. what version are you using ? im using 6.2.3 and another project using 15.0.2, both working fine in android.. try check you payload.. using custom_notification, show_in_foreground to true – Hazim Ali May 20 '18 at 14:48
  • @HazimAli hey, I'm using cloud functions to send a notification to specific Token, and it's received on the device very well, but when the user open this notify, it's just open the app, how can I specific this to open a special screen? Like Notifications screen? –  Jul 23 '19 at 11:42