Flutter-在onBackgroundMessage上安装firebase_messaging后发生RuntimeException

时间:2020-02-08 16:38:05

标签: android flutter firebase-cloud-messaging

我遵循了firebase_messaging自述文件,并且可以运行firebase消息传递而无需onBackgroundMessage很好。 当我尝试通过遵循自述文件,创建Application.java,更改程序包和AndroidManifest来设置后台消息时,出现以下内容,并且应用程序在启动时崩溃:

2020-02-08 16:20:29.064 11210-11210/company.app/AndroidRuntime: FATAL EXCEPTION: main
Process: company.app, PID: 11210
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{company.app/company.app.Application}: java.lang.ClassCastException: company.app.Application cannot be cast to android.app.Activity
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
 Caused by: java.lang.ClassCastException: company.app.Application cannot be cast to android.app.Activity
    at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
    at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1243)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:214) 
    at android.app.ActivityThread.main(ActivityThread.java:7356) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

MainActivity文件夹上的Application.java:

package company.app;
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
    @Override
    public void onCreate() {
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

@Override
public void registerWith(PluginRegistry registry) {
    GeneratedPluginRegistrant.registerWith(registry);
}

}

Pubspec.yaml:

environment:
  sdk: ">=2.2.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.2
  firebase_messaging: ^6.0.9
  firebase_auth: ^0.14.0+5
  firebase_database: ^3.0.7
  cloud_firestore: ^0.12.9+6
  audioplayers: ^0.14.0
  flutter_spinkit: "^2.1.0"
  intl: ^0.16.1
  flutter_ringtone_player: ^2.0.0

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:

  uses-material-design: true

  assets:
    - assets/images/
    - assets/sounds/

扑打医生:

    [√] Flutter (Channel stable, v1.12.13+hotfix.7, on Microsoft Windows [Version 10.0.18363.592], locale pt-PT)
    • Flutter version 1.12.13+hotfix.7 at C:\Program Files\Flutter
    • Framework revision 9f5ff2306b (12 days ago), 2020-01-26 22:38:26 -0800
    • Engine revision a67792536c
    • Dart version 2.7.0

[!] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at C:\Users\Guilherme\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses

[√] Android Studio (version 3.5)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 43.0.1
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)

[√] VS Code (version 1.42.0)
    • VS Code at C:\Users\Guilherme\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.8.1

[√] Connected device (1 available)
    • Android SDK built for x86 • emulator-5554 • android-x86 • Android 10 (API 29) (emulator)

! Doctor found issues in 1 category.

1 个答案:

答案 0 :(得分:1)

更改此方法:

@Override
public void registerWith (PluginRegistry registry) {
     GeneratedPluginRegistrant.registerWith (registry);
}

为此:

@Override
     public void registerWith (PluginRegistry registry) {
         FirebaseMessagingPlugin.registerWith (registry.registrarFor ("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
     }