【问题标题】:Crash when receive notification if app is closed/swipe ( React Native )如果应用程序关闭/滑动(React Native)收到通知时崩溃
【发布时间】:2018-07-16 10:28:49
【问题描述】:

我有一个使用 Firebase 推送通知的应用,当应用在前台和后台收到通知时,它可以正常工作。但是当我关闭/滑动应用程序时,它会崩溃。

这是关于崩溃的日志:

    --------- beginning of crash
07-16 17:16:56.218 7552-7571/? E/AndroidRuntime: FATAL EXCEPTION: Firebase-RNFirebaseMessagingService
    Process: com.tutorial, PID: 7552
    java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.tutorial. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(Unknown Source:53)
        at com.google.firebase.messaging.zzb.zzb(Unknown Source:191)
        at com.google.firebase.messaging.zzb.zzc(Unknown Source:2)
        at com.google.firebase.messaging.FirebaseMessagingService.zzd(Unknown Source:353)
        at com.google.firebase.iid.zzg.run(Unknown Source:26)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at com.google.android.gms.common.util.concurrent.zza.run(Unknown Source:7)
        at java.lang.Thread.run(Thread.java:764)
07-16 17:17:05.703 7552-7569/com.tutorial W/EnhancedIntentService: Service took too long to process intent: com.google.android.c2dm.intent.RECEIVE App may get closed.

在我的应用程序中,我实现了 2 种方法:

this.notificationListener = firebase.notifications().onNotification((notification) => {

                senName = notification.data.senderName;
                senUid = notification.data.senderUid;

                const showNotif = new firebase.notifications.Notification()
                    .setNotificationId('notificationId')
                    .setTitle(notification.title)
                    .setBody(notification.data.text)
                    .android.setChannelId('channel_id_foreground')
                    .android.setSmallIcon('ic_launcher');
                firebase.notifications().displayNotification(showNotif)

            });

    this.notificationOpenListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
        // Get the action triggered by the notification being opened
        const action = notificationOpen.action;
        // Get information about the notification that was opened
        const notification = notificationOpen.notification;

        if (!senName || !senUid) {
            senName = notification.data.senderName;
            senUid = notification.data.senderUid;
        }

        var roomUid = this.generateChatId(firebase.auth().currentUser.uid, senUid);
        this.goToChatRoomWithId('ChatScreen', senName, senUid, roomUid);
    });

有人知道这个问题吗?

【问题讨论】:

    标签: firebase react-native push-notification firebase-cloud-messaging


    【解决方案1】:

    我认为您正在使用react-native-firebase lib。

    图书馆信息

    简介 RNFirebase 使使用 Firebase 和 React Native 变得简单。它是一个轻量级层,位于 iOS 和 Android 的原生 Firebase 库之上,尽可能地反映 Firebase Web SDK。

    虽然 Firebase Web SDK 库可以与 React Native 一起使用,但它主要是为 Web 构建的。

    RNFirebase 为 iOS 和 Android 的原生 Firebase SDK 提供了一个 JavaScript 桥接,因此 Firebase 将在原生线程上运行,从而允许您应用的其余部分在 JS 线程上运行。 Firebase Web SDK 也在 JS 线程上运行,因此可能会影响帧速率,从而导致动画、触摸事件等卡顿。

    原生 SDK 还允许我们连接到 Web SDK 无法实现的设备 sdk,例如崩溃报告、离线实时数据库支持、分析等等!

    总而言之,RNFirebase 提供了比 web SDK 更快的性能(约 2 倍),并提供了 web sdk 中没有的设备 sdk(请参阅下面的功能表)。

    我的推荐: react-native-fcm

    【讨论】:

    • 感谢您的有用建议,我终于解决了这个问题。只需稍作改动:classpath 'com.google.gms:google-services:4.0.1'classpath 'com.google.gms:google-services:3.2.1'
    • react-native-fcm 建议使用react-native-firebase
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 2019-09-04
    • 1970-01-01
    相关资源
    最近更新 更多