1

仅在应用程序打开,前台模式时收到推送通知时出现此错误!

在使用 Firebase 推送通知和 react-native-fcm 之前有人遇到过这个问题吗?

代码:

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

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()
    );
  }
}

我得到的完整错误:

java.lang.ClassCastException:fi.rogerstudio.possis.MainApplication 无法在 com.evollu.react.fcm.MessagingService$1.run(MessagingService.java:41) 在 android.os.Handler 上转换为 com.facebook.react.ReactApplication .handleCallback(Handler.java:751) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread. java:6682) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 在 com.android.internal.os.ZygoteInit。主要(ZygoteInit.java:1410)

4

1 回答 1

0

对于可能会在事实发生 3.5 年后出现这种情况的人,解决方案是确保您的主应用程序扩展 ReactApplication,

public class MainApplication extends MultiDexApplication, ReactApplication

然后getReactNativeHost像这样实现:

public class MainApplication extends MultiDexApplication {
  ...
  private final ReactNativeHost mReactNativeHost =
      new ReactNativeHost(this) {
        @Override
        public boolean getUseDeveloperSupport() {
          return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
          // Packages that cannot be autolinked yet can be added manually here, for example:
          // packages.add(new MyReactNativePackage());
          return packages;
        }

        @Override
        protected String getJSMainModuleName() {
          return "index";
        }
      };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }
 ...
}
于 2021-09-28T00:29:22.200 回答