我有一个应用程序,它使用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);
});
有人知道这个问题吗?
我认为您使用的是react-native-firebase
库。
库信息:
引言RNFirebase使使用带有React Native的Firebase变得简单。它是一个轻量级层,位于iOS和Android的原生Firebase库之上,尽可能地镜像Firebase Web SDK。
尽管Firebase Web SDK库将与React Native一起使用,但它主要是为Web构建的
RNFirebase为iOS和Android的原生Firebase SDK提供了一个JavaScript桥,因此Firebase将在原生线程上运行,允许您的应用程序的其余部分在JS线程上运行。Firebase Web SDK也在JS线程上运行,因此可能会影响帧速率,导致动画、触摸事件等出现jank。
原生sdk还允许我们连接到web sdk无法连接到的设备sdk,例如崩溃报告、离线实时数据库支持、分析等!
总而言之,RNFirebase比web SDK提供了更快的性能(约2倍(,并提供了web SDK中找不到的设备SDK(请参阅下面的功能表(。
我的推荐:反应原生fcm