我有一个使用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);
});
有人知道这个问题吗?
答案 0 :(得分:0)
我认为您正在使用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还使我们能够连接到设备SDK中,而这是Web SDK无法实现的,例如崩溃报告,离线实时数据库支持,分析等等!
总而言之,RNFirebase在Web SDK上提供了更快的性能(〜2倍),并提供了在Web sdk中找不到的设备sdk(请参见下面的功能表)。
我的建议: react-native-fcm