当应用程序打开时,React Native Firebase 推送通知不起作用

Rah*_*dev 5 react-native react-native-firebase

我正在使用@react-native-firebase来处理推送通知。问题是当应用程序在后台时它正在工作。如果应用程序处于前台或打开状态,则推送通知不起作用。另外,我正在使用notifee创建自定义通知。它也不起作用。

这是版本详细信息。

"@react-native-firebase/app": "^8.3.1",
"@react-native-firebase/messaging": "^7.7.2",
"@notifee/react-native": "^0.12.3",
Run Code Online (Sandbox Code Playgroud)

实施代码

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);
    }
Run Code Online (Sandbox Code Playgroud)

小智 -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);
      });
Run Code Online (Sandbox Code Playgroud)

使用此通知监听器

欲了解更多信息: https: //rnfirebase.io/messaging/notifications