使用 react-native-firebase v6,应用程序在前台接收通知时崩溃

With react-native-firebase v6, app crashed when receive notification while in foreground

我尝试让 react-native-firebase v6 在我的应用程序中运行。我使用 React Native 0.59.10.

我已经按照documentation安装了react-native-firebasev6。与 v5 不同,它没有指定将服务 MyFirebaseMessagingService 添加到 AndroidManifest.xml 中,所以我没有这样做。之后,该应用程序在前台没有收到任何通知,但在后台收到了。

我尝试将 MyFirebaseMessagingService 添加到 AndroidManifest.xml 中,如下所示:

<service
    android:name=".java.MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

有一些进展。在我从 Firebase 控制台发送通知后,应用程序立即崩溃。因此,我知道该应用程序知道收到的通知,但不知何故崩溃了。

下面是我导入和初始化侦听器的代码。

import messaging from '@react-native-firebase/messaging';
import { Alert } from 'react-native';

// Initialize notifications
const init = () => {
    try {
        messaging().onMessage((message) => {
            Alert.alert('Received', JSON.stringify(message));
        });
    } catch (err) {
        Alert.alert('Error', err.message);
    }
};

总而言之,我希望在应用程序处于前台时收到通知,但如果我不将 MyFirebaseMessagingService 添加到 AndroidManifest.xml,则不会发生任何事情。如果我添加它,应用程序将在收到通知时崩溃。

好的。经过进一步的研究和研究,我发现这是预期的行为,而不是错误。

无需将 MyFirebaseMessagingService 或任何其他服务添加到 AndroidManifest.xml 即可安装 react-native-firebase v6。

Afterwards, the app didn't receive any notification while in foreground but did receive them while in background.

事实上,确实如此。使用 adb logcat -s RNFirebaseMsgService:V 查看 Android 日志后,我发现 react-native-firebase v6 目前不支持通知。这是他们的代码,让我觉得我的应用程序在前台时没有收到任何通知。

if (remoteMessage.getNotification() != null && remoteMessage.getData().size() == 0) {
    // TODO broadcast intent when notifications module ready
    return;
}

这对他们lmao来说仍然是一个待办事项!!

原来我需要发送一个带有自定义数据的通知,因为它已经被支持了。因此,我每次都需要使用它,以便我的应用程序可以在收到此类通知时显示任何内容。