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

时间:2019-06-10 19:46:41

标签: react-native

当我将测试消息从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
    }
}


1 个答案:

答案 0 :(得分:0)

我错过了AndroidManifest.xml文件中的代码。如果遇到相同问题,请检查此文件。