0
votes

I added firebase push notifications to my app.js file as following:

import messaging from '@react-native-firebase/messaging';

async function registerAppWithFCM() {
  await messaging().registerDeviceForRemoteMessages();
}

//inside app function:
const unsubscribe = messaging().onMessage(async remoteMessage => {
  console.log("Foreground message");
})

messaging().setBackgroundMessageHandler(async remoteMessage => {
  console.log('Message handled in the background!', remoteMessage);
});

Then i sent a notification from the server while app in foreground and it worked (logged "Foreground message"), when i tried it again while app on the background the notification have arrived and logged (Message handled in the background! and the remote message) but when clicked on it to open app it crashed the app (App has stopped), i am using :

"@react-native-firebase/app": "^6.4.0", "@react-native-firebase/messaging": "^6.4.0", "react": "16.11.0", "react-native": "0.62.0",

1
it can help if you dump a log of the crash also here so we can know what kind of problem is - Rinor Dreshaj
Hi, Did you get solution for this - loyoliteabid

1 Answers

0
votes
messaging().onNotificationOpenedApp(remoteMessage => {
      console.log(
        'Notification caused app to open from background state:',
        remoteMessage.notification,
      );
      navigation.navigate(remoteMessage.data.type);
    });

    // Check whether an initial notification is available
    messaging()
      .getInitialNotification()
      .then(remoteMessage => {
        if (remoteMessage) {
          console.log(
            'Notification caused app to open from quit state:',
            remoteMessage.notification,
          );
          setInitialRoute(remoteMessage.data.type); // e.g. "Settings"
        }
        setLoading(false);
      });

Use this notification listeners

https://rnfirebase.io/messaging/notifications