5

I am using @react-native-firebase for handling push notification. Problem is that It is working when the App is in the background. If the App is in the foreground or open state, Push notification not working. Also, I am using notifee to create a custom notification. It also not working.

here are the version details.

"@react-native-firebase/app": "^8.3.1",
"@react-native-firebase/messaging": "^7.7.2",
"@notifee/react-native": "^0.12.3",

Implemented Code

componentDidMount() {
        console.log('componentDidMount App ');

        // AppState.addEventListener('change', this._handleAppStateChange);

        messaging()
            .getInitialNotification()
            .then(remoteMessage => {
                console.log('Notification caused app to open from quit state:');
                if (remoteMessage) {
                    console.log(remoteMessage.notification);
                }
            });

        const unsubscribe = messaging().onMessage(async remoteMessage => {
            console.log('A new FCM message arrived!');
            console.log(remoteMessage);
        });

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

        });

        messaging().onNotificationOpenedApp(remoteMessage => {
            console.log('Notification caused app to open from background state:');
            console.log(remoteMessage);
            Actions.AddMeeting({
                actionType: 'add',
            });
            // navigation.navigate(remoteMessage.data.type);
        // });

        messaging().registerDeviceForRemoteMessages();

        notifee.registerForegroundService(() => {
            return new Promise(resolve => {
                // Long running task...

            });
        });

        const channelId = notifee.createChannel({
            id: 'default',
            name: 'Default Channel',
        });

        async function onMessageReceived(message) {
            // Do something
            // notifee.displayNotification(message.data.msg);
            console.log('onMessageReceived');
            console.log(JSON.stringify(message));
            notifee.displayNotification({
                title: 'Foreground service',
                body: 'This notification will exist for the lifetime of the service runner',
                android: {
                    channelId,
                    asForegroundService: true,
                    color: '#dc193c',
                    colorized: true,
                },
            });
        }

        messaging().onMessage(onMessageReceived);
        messaging().setBackgroundMessageHandler(onMessageReceived);
    }
Rahul dev
  • 1,267
  • 3
  • 18
  • 31

1 Answers1

-2
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

For more info: https://rnfirebase.io/messaging/notifications

Vinit Bhavsar
  • 226
  • 3
  • 13