【问题标题】:With react-native-firebase v6, app crashed when receive notification while in foreground使用 react-native-firebase v6,应用程序在前台收到通知时崩溃
【发布时间】:2019-10-24 04:00:46
【问题描述】:

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

我已经按照documentation安装了react-native-firebase v6。与 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,则不会发生任何事情。如果我添加它,应用程序将在收到通知时崩溃。

【问题讨论】:

  • 发布堆栈跟踪。
  • @AbhayKoradiya 我已经找到了解决方案。谢谢。
  • @Dorklord 你能帮我在 v6 中显示本地通知吗?

标签: android firebase react-native react-native-firebase


【解决方案1】:

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

添加MyFirebaseMessagingService 或任何其他服务到AndroidManifest.xml 来安装react-native-firebase v6 是不必要的。

之后,应用在前台没有收到任何通知,但在后台收到了通知。

事实上,确实如此。在使用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!

结果我需要发送带有自定义数据的通知,因为它已经受支持。因此,我每次都需要使用它,这样我的应用程序就可以在收到此类通知时显示任何内容。

【讨论】:

  • 老兄,在找到您的答案之前,我已经为同样的问题苦苦挣扎了 2 天。你是我的天赐之物,非常感谢!如果有人想测试“数据类型”消息传递,您可以使用 curl/postman 发送请求:stackoverflow.com/a/37376757/3175823
  • @fotonmoton 很高兴我能提供帮助。摇滚吧!
猜你喜欢
  • 2021-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多