从 firebase 控制台接收消息时,应用程序崩溃(<appName> 已停止)

西瓦拉姆

当我从 firebase 控制台向我的设备发送测试消息时,应用程序崩溃,没有任何控制台日志。

安装的软件包:

  • “反应”:“16.6.3”
  • “反应原生”:“^0.59.8”
  • "react-native-device-info": "^1.4.3"
  • "react-native-firebase": "^5.3.1"
  • "react-native-paper": "^2.12.0"
  • "react-native-vector-icons": "^6.3.0"

build.gradle // Firebase 依赖项

implementation "com.google.android.gms:play-services-base:16.1.0" 
implementation "com.google.android.gms:play-services-auth:16.0.1" 
implementation "com.google.firebase:firebase-core:16.0.9"
implementation "com.google.firebase:firebase-messaging:18.0.0"

已经尝试过的事情:

adb logcat - no use
adb logcat *:S ReactNative:V ReactNativeJS:V - no use


//App.js code for receiving remote notification

async componentDidMount() {
    this.checkPermission();
    this.onTokenRefreshListener = firebase.messaging().onTokenRefresh(fcmToken => {
        // Process your token as required
        Alert.alert(fcmToken);
    });
    this.messageListener = firebase.messaging().onMessage((message) => {
        console.log("JSON.stringify:", JSON.stringify(message));
        Alert.alert(message);
    });
    this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
        const { title, body } = notification;
        console.log('onNotification:');

        const localNotification = new firebase.notifications.Notification({

          show_in_foreground: true,
        })
        .setNotificationId(notification.notificationId)
        .setTitle(notification.title)
        .setBody(notification.body)
        .android.setChannelId('fcm_FirebaseNotifiction_default_channel') // e.g. the id you chose above
        .android.setSmallIcon('@drawable/ic_launcher') // create this icon in Android Studio
        .android.setColor('#000000') // you can set a color here
        .android.setPriority(firebase.notifications.Android.Priority.High);

        firebase.notifications()
          .displayNotification(localNotification)

    });
    this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
        console.log("test");
    });
    this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
        // Get the action triggered by the notification being opened
        const action = notificationOpen.action;
        // Get information about the notification that was opened
        const notification: Notification = notificationOpen.notification;
    });
    const notificationOpen = await firebase.notifications().getInitialNotification();
    if (notificationOpen) {
      const { title, body } = notificationOpen.notification;
      console.log('getInitialNotification:');
      Alert.alert(title, body)
    }
}

componentWillUnmount() {
    try {
        this.onTokenRefreshListener();
        this.messageListener();
        this.notificationListener();
        this.notificationOpenedListener();
    } catch (error) {
        console.error(error)
    }

}

async checkPermission(){
    const enabled = await firebase.messaging().hasPermission();
    if (enabled) {
        this.getToken();
    } else {
        this.requestPermission();
    }
}
async getToken(){
    const fcmToken = await firebase.messaging().getToken();
    if (fcmToken) {
        console.log("FCMToken:"+fcmToken);
        this.setState({test:fcmToken})
    } else {
        // user doesn't have a device token yet
    }
}

async requestPermission(){
    try {
        await firebase.messaging().requestPermission();
        // User has authorised
    } catch (error) {
        // User has rejected permissions
    }
}


西瓦拉姆

我错过了 AndroidManifest.xml 文件中的代码。如果有人面临同样的问题,请检查此文件。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Firebase控制台中的项目已消失,但应用程序运行正常

Google Play控制台-如何删除已发布应用程序的更新

完全禁用Firebase / Analytics(分析)以在应用启动时停止控制台垃圾邮件

Firebase:如何从Firebase控制台检查特定应用程序版本上的用户数

Microsoft管理控制台已停止工作

选择控制台停止应用程序

如何为已编译的Haskell GUI应用程序摆脱黑色控制台窗口

重新打开已关闭或使用控制台应用程序取消活动

如何停止控制台应用程序中的闪烁?

Firebase控制台是否已死(未显示项目)?

单击搜索时,我的应用程序已停止

应用程序已停止工作

不幸的是appName已停止错误

控制台应用程序Args到任务计划程序参数(已关闭)

解决“应用程序已停止”崩溃错误

更改默认布局并说“不幸的是,应用程序已停止”时,应用程序崩溃

不幸的是,应用程序已停止

在模拟器中启动我的第一个应用程序会导致“不幸的是,AppName已停止”

Eclipse中的Android项目,不幸的是'appName'已停止

不幸的是,appname已停止

为什么在提示文字时C#控制台应用程序会停止运行?

.Net Core控制台应用程序-调试失败-与远程端点的连接已终止

当我按下 ButtonImage 时应用程序崩溃。不幸的是,该应用程序已停止工作

如何停止、重新启动或查看启动应用程序的控制台日志?

如何跟踪 C++14 中的控制台应用程序已启动的次数?

已发布的 C# 控制台应用程序 - 系统找不到指定的文件

从 .Net Core 控制台应用程序更新 Firebase Firestore 中的文档

Flask 应用程序中的记录器已停止记录到控制台

监听按键 .NET 控制台应用程序不会停止运行应用程序