Skip to content

🐛 [firebase_messaging] FCM Integration App Crashed on ClassCastException #3881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Scienticious opened this issue Oct 16, 2020 · 3 comments · Fixed by #4012
Closed

🐛 [firebase_messaging] FCM Integration App Crashed on ClassCastException #3881

Scienticious opened this issue Oct 16, 2020 · 3 comments · Fixed by #4012
Labels
Needs Attention This issue needs maintainer attention. type: bug Something isn't working

Comments

@Scienticious
Copy link

Bug report

I am trying to implement Firebase push notifications in background & I am following the Official Guidelines
When I run the code error has been showing and application is crashing

A clear and concise description of what the bug is.
I have pasted the background messaging code in application file in kotlin

import io.flutter.app.FlutterApplication
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback

public class Application: FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }
    override fun registerWith(registry: PluginRegistry) {
       FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }
}    

Crash Log

/AndroidRuntime(31418): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.firebase_push_notification_my/com.example.firebase_push_notification_my.Application}: java.lang.ClassCastException: com.example.firebase_push_notification_my.Application cannot be cast to android.app.Activity E/AndroidRuntime(31418): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2874) E/AndroidRuntime(31418): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3079) E/AndroidRuntime(31418): at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) E/AndroidRuntime(31418): at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) E/AndroidRuntime(31418): at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) E/AndroidRuntime(31418): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1836) E/AndroidRuntime(31418): at android.os.Handler.dispatchMessage(Handler.java:106) E/AndroidRuntime(31418): at android.os.Looper.loop(Looper.java:193) E/AndroidRuntime(31418): at android.app.ActivityThread.main(ActivityThread.java:6702) E/AndroidRuntime(31418): at java.lang.reflect.Method.invoke(Native Method) E/AndroidRuntime(31418): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) E/AndroidRuntime(31418): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911) E/AndroidRuntime(31418): Caused by: java.lang.ClassCastException: com.example.firebase_push_notification_my.Application cannot be cast to android.app.Activity E/AndroidRuntime(31418): at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69) E/AndroidRuntime(31418): at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41) E/AndroidRuntime(31418): at android.app.Instrumentation.newActivity(Instrumentation.java:1215) E/AndroidRuntime(31418): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2862)

Steps to reproduce

Run the App, and it will crash

Expected behavior

App has to been run and show the notification when notification come


Additional context

The Android side code must be provided in Kotlin instead of Java


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 1.22.1, on Linux, locale en_US.UTF-8)
 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[✓] Android Studio (version 4.0)
[!] IntelliJ IDEA Community Edition (version 2019.3)
    ✗ Flutter plugin not installed; this adds Flutter specific functionality.
    ✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] Connected device (1 available)

! Doctor found issues in 1 category.


Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand
Dart SDK 2.10.1
Flutter SDK 1.22.1
firebase_push_notification_my 1.0.0+1

dependencies:
- cupertino_icons 1.0.0
- firebase_messaging 7.0.3 [meta flutter firebase_core]
- flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine]

dev dependencies:
- flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_scanner term_glyph typed_data]

transitive dependencies:
- async 2.5.0-nullsafety.1 [collection]
- boolean_selector 2.1.0-nullsafety.1 [source_span string_scanner]
- characters 1.1.0-nullsafety.3
- charcode 1.2.0-nullsafety.1
- clock 1.1.0-nullsafety.1
- collection 1.15.0-nullsafety.3
- fake_async 1.2.0-nullsafety.1 [clock collection]
- firebase 7.3.2 [http http_parser js]
- firebase_core 0.5.0+1 [firebase_core_platform_interface flutter quiver meta firebase_core_web]
- firebase_core_platform_interface 2.0.0 [flutter meta plugin_platform_interface quiver]
- firebase_core_web 0.2.0 [firebase firebase_core_platform_interface flutter flutter_web_plugins meta js]
- flutter_web_plugins 0.0.0 [flutter characters collection meta typed_data vector_math]
- http 0.12.2 [http_parser path pedantic]
- http_parser 3.1.4 [charcode collection source_span string_scanner typed_data]
- js 0.6.2
- matcher 0.12.10-nullsafety.1 [stack_trace]
- meta 1.3.0-nullsafety.3
- path 1.8.0-nullsafety.1
- pedantic 1.9.2 [meta]
- plugin_platform_interface 1.0.3 [meta]
- quiver 2.1.3 [matcher meta]
- sky_engine 0.0.99
- source_span 1.8.0-nullsafety.2 [charcode collection path term_glyph]
- stack_trace 1.10.0-nullsafety.1 [path]
- stream_channel 2.1.0-nullsafety.1 [async]
- string_scanner 1.1.0-nullsafety.1 [charcode source_span]
- term_glyph 1.2.0-nullsafety.1
- test_api 0.2.19-nullsafety.2 [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner term_glyph matcher]
- typed_data 1.3.0-nullsafety.3 [collection]
- vector_math 2.1.0-nullsafety.3


@Scienticious Scienticious added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Oct 16, 2020
@qoob23
Copy link

qoob23 commented Oct 17, 2020

I had same issue and found out that if I omit steps 2, 3 and 4 from Official Guidelines, everything works as expected. I think this is because plugin (or flutter) generates all necessary code in android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java

After installing firebase messaging, next code appeared inside registerWith function:
flutterEngine.getPlugins().add(new io.flutter.plugins.firebase.core.FlutterFirebaseCorePlugin()); flutterEngine.getPlugins().add(new io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin());

@Scienticious
Copy link
Author

@Svet-00 , are background messaging working in your app if you omit step 2,3,4 ?

@TahaTesser
Copy link

Hi @Scienticious
Please try this, has no issues when running

Application

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

    class Application : FlutterApplication(), PluginRegistrantCallback {

        override fun onCreate() {
            super.onCreate()
            FlutterFirebaseMessagingService.setPluginRegistrant(this);
            FlutterMain.startInitialization(this)
        }

        override fun registerWith(registry: PluginRegistry?) {
            if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) {
                FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
            }
        }
    }

MainActviity

import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
import io.flutter.view.FlutterMain

class MainActivity : FlutterActivity(), PluginRegistry.PluginRegistrantCallback {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
        FlutterMain.startInitialization(this)
    }

    override fun registerWith(registry: PluginRegistry?) {
        if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) {
            FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        }
    }

}

There are a bunch of issues for kotlon example in messaging #2311 #3411
Closing as duplicate

@firebase firebase locked and limited conversation to collaborators Nov 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs Attention This issue needs maintainer attention. type: bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants