【问题标题】:FCM : Unable to instantiate receiver com.google.firebase.iid.FirebaseInstanceIdReceiver:FCM:无法实例化接收器 com.google.firebase.iid.FirebaseInstanceIdReceiver:
【发布时间】:2018-05-17 13:27:45
【问题描述】:

仅在 Oreo(8.0) 中接收推送通知时应用崩溃。

java.lang.RuntimeException: Unable to instantiate receiver com.google.firebase.iid.FirebaseInstanceIdReceiver: java.lang.ClassCastException: android.content.ContextWrapper cannot be cast to android.app.ContextImpl
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3236)
        at android.app.ActivityThread.-wrap17(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1677)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)

【问题讨论】:

  • 如果您不在代码中显示崩溃发生的位置,几乎不可能回答您的问题。
  • 我只得到上面的异常,代码中没有错误/异常。由于崩溃仅在收到通知时出现在 oreo 8 版本中,因此我怀疑以下行。 String refreshedToken = FirebaseInstanceId.getInstance().getToken();

标签: android firebase firebase-cloud-messaging android-8.0-oreo


【解决方案1】:

我在使用书法库时遇到了同样的问题。当 Firebase 第一次请求令牌时,它甚至在安装后第一次崩溃。不仅接收推送通知。

我的错误是我在 Application 类中重写 attachBaseContext 方法以返回 ContextWrapper 对象,但仅在活动中需要。

所以要恢复我的解决方案是删除 Application 类中的 attachBaseContext 并将其移动或只是将其放入活动中。

【讨论】:

    【解决方案2】:

    这可能会有所帮助;如果您正在使用它,请将这些添加到您的 proguard!

    -dontwarn com.google.android.gms.**
    -keep class com.google.android.gms.** { *; }
    -keep class com.google.firebase.** { *; }
    

    【讨论】:

    • 谢谢!这就是我在尝试启用 R8 进行构建时为我解决的问题。
    • 这也为我修好了!
    • 建议编辑:将新文件“proguard.cfg”添加到 Xamarin.Android 项目(根),粘贴上面建议的配置,将“proguard.cfg”的构建操作设置为“ProguardConfiguration”。
    【解决方案3】:

    我刚刚从 Application 类中删除了 attachBaseContext

        override fun attachBaseContext(newBase: Context) {
        val lang: String = newBase.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE)
            .getString(KEY_LANG, ENGLISH) ?: ENGLISH
        super.attachBaseContext(ApplicationContextWrapper.wrap(newBase, lang))
    }
    

    【讨论】:

      【解决方案4】:

      为了解决“无法实例化接收器”的问题,我在 proguard.cfg 中添加了上述行。我使用 d8 Dex 编译器和 r8 编译代码收缩器。然而问题并没有解决。 所以我将 Code shrinker 切换为 proguard。但是,不再支持 ProGuard 与 d8 Dex 编译器结合使用。所以我将 Dex 编译器切换到 dx。你猜怎么着?

      问题已解决:应用不再崩溃。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多