应用程序在背景上接收推送通知时工作得非常好,但在foregrond时它会崩溃 . 我正在使用RN 0.49和react-native-fcm 11.3.1包 .

我的代码如下所示:

FCM.on(FCMEvent.Notification, async (notif) => {
    // there are two parts of notif. notif.notification contains the notification payload, notif.data contains data payload
    if(notif.local_notification){
        // return;
        console.log("1111");
        this.showLocalNotification(notif);
    }
    if(notif.opened_from_tray){
      //iOS: app is open/resumed because user clicked banner
      //Android: app is open/resumed because user clicked banner or tapped app icon
      //   return;
        console.log("2222");
        this.showLocalNotification(notif);

    }
    // await someAsyncCall();

    if(Platform.OS ==='ios'){
      //optional
      //iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application.
      //This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override
      //notif._notificationType is available for iOS platfrom
      switch(notif._notificationType){
        case NotificationType.Remote:
          notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
          break;
        case NotificationType.NotificationResponse:
          notif.finish();
          break;
        case NotificationType.WillPresent:
          notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
          break;
      }
    }
});

FCM.on(FCMEvent.RefreshToken, (token) => {
    console.log("---------Refresh Token-------------")
    console.log(token)
    // fcm token may not be available on first load, catch it here
});

showLocalNotification(notif) {
        FCM.presentLocalNotification({
            title: notif.title,
            body: notif.body,
            priority: "high",
            click_action: notif.click_action,
            show_in_foreground: true,
            local: true
        });
    }

componentDidMount(){
        // iOS: show permission prompt for the first call. later just check permission in user settings
        // Android: check permission in user settings
        FCM.requestPermissions().then(()=>console.log('granted')).catch(()=>console.log('notification permission rejected'));

        FCM.getFCMToken().then(token => {
            console.log(token)
            // store fcm token in your server
        });

        this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
            console.warn("listening.......................");
        });

        // initial notification contains the notification that launchs the app. If user launchs app by clicking banner, the banner notification info will be here rather than through FCM.on event
        // sometimes Android kills activity when app goes to background, and when resume it broadcasts notification before JS is run. You can use FCM.getInitialNotification() to capture those missed events.
        // initial notification will be triggered all the time even when open app by icon so send some action identifier when you send notification
        FCM.getInitialNotification().then(notif => {
           console.log(notif)
        });
    }

MainApplication.java

public class MainApplication extends MultiDexApplication {

  // Needed for `react-native link`
  public List<ReactPackage> getPackages() {
    return Arrays.<ReactPackage>asList(
        // Add your own packages here!
        // TODO: add cool native modules
//            new MainReactPackage(),

            new RNBackgroundGeolocation(),
            // Needed for `react-native link`

            new FIRMessagingPackage(),
            //new RNBackgroundGeolocation(),
            //new RNFirebasePackage(),
            new VectorIconsPackage()
            //new RNFirebaseMessagingPackage()
    );
  }
}

Android Studio会出现此错误:

java.lang.ClassCastException:xyzMainApplication无法在android.os.Handler.handleCallback(Handler)的com.evollu.react.fcm.MessagingService $ 1.run(MessagingService.java:41)上转换为com.facebook.react.ReactApplication . .java:751)在android.app.A.运行时,android.O.Roper.loop(Looper.java:154)上的android.os.Handler.dispatchMessage(Handler.java:95) . (ActivityThread.java:6682) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1520)的com.android.internal.os.ZygoteInit.main(ZygoteInit . )中的java.lang.reflect.Method.invoke(Native Method) . Java的:1410)

我不知道是什么导致应用程序在应用程序打开时仅在前台状态崩溃!有关如何解决这个问题的任何想法?