skip to Main Content

I have a react native app and installed react native app & messaging, and can receive notifications, but what i need is incoming call notification, so i implemented handler to first catch the notification when app is closed, but even my log not appear in console and device just shows nodification and get fired when app is in forground, why is that?

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

2

Answers


  1. Chosen as BEST ANSWER

    my issue was that i test it with fcm tester, notification must be data only type to not be shown


  2. If want to see foreground notification messages then you have to use onMessage listener

    Foreground state messages

    import React, { useEffect } from 'react';
    import { Alert } from 'react-native';
    import messaging from '@react-native-firebase/messaging';
    
    function App() {
      useEffect(() => {
        const unsubscribe = messaging().onMessage(async remoteMessage => {
          Alert.alert('A new FCM message arrived!', JSON.stringify(remoteMessage));
        });
    
        return unsubscribe;
      }, []);
    }
    

    Background & Quit state messages

    Important note: To setup a background handler, call the setBackgroundMessageHandler outside of your application logic as early as possible

    // index.js
    import { AppRegistry } from 'react-native';
    import messaging from '@react-native-firebase/messaging';
    import App from './App';
    
    // Register background handler
    messaging().setBackgroundMessageHandler(async remoteMessage => {
      console.log('Message handled in the background!', remoteMessage);
    });
    
    AppRegistry.registerComponent('app', () => App);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search