我正在尝试注册Firebase Cloud Messaging,以便在后台运行它并接收通知。
问题陈述: 该应用程序运行正常,并且毫无例外地接收了以下国家的通知和数据。
4. onBackgroundMessage -当我引入状态 onBackgroundMessage 以便在后台处理通知时,它会引发期望,并且在接收通知时应用程序在 onBackgroundMessage 上崩溃
我研究了不同的StackOverflow问题。其中之一是我可以获取Kotlin代码的地方。
但这给了我这个错误,我无法理解该错误以及如何解决。
异常(在Flutter控制台中)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 8516): Failed to handle method calls
E/MethodChannel#plugins.flutter.io/firebase_messaging( 8516): java.lang.RuntimeException: PluginRegistrantCallback is not set.
E/MethodChannel#plugins.flutter.io/firebase_messaging( 8516): at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:157)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 8516): at io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.onMethodCall(FirebaseMessagingPlugin.java:174)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 8516): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 8516): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 8516): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:693)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 8516): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 8516): at android.os.MessageQueue.next(MessageQueue.java:336)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 8516): at android.os.Looper.loop(Looper.java:174)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 8516): at android.app.ActivityThread.main(ActivityThread.java:7356)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 8516): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 8516): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 8516): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
E/flutter ( 8516): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: PlatformException(error, PluginRegistrantCallback is not set., null, java.lang.RuntimeException: PluginRegistrantCallback is not set.
E/flutter ( 8516): at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:157)
E/flutter ( 8516): at io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.onMethodCall(FirebaseMessagingPlugin.java:174)
E/flutter ( 8516): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/flutter ( 8516): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/flutter ( 8516): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:693)
E/flutter ( 8516): at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter ( 8516): at android.os.MessageQueue.next(MessageQueue.java:336)
E/flutter ( 8516): at android.os.Looper.loop(Looper.java:174)
E/flutter ( 8516): at android.app.ActivityThread.main(ActivityThread.java:7356)
E/flutter ( 8516): at java.lang.reflect.Method.invoke(Native Method)
E/flutter ( 8516): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/flutter ( 8516): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
E/flutter ( 8516): )
E/flutter ( 8516): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:582:7)
E/flutter ( 8516): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:159:18)
E/flutter ( 8516): <asynchronous suspension>
E/flutter ( 8516): #2 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:332:12)
E/flutter ( 8516): #3 FirebaseMessaging.configure (package:firebase_messaging/firebase_messaging.dart:126:16)
E/flutter ( 8516): #4 PushNotificationsManager.init (package:flutter_app/services/pushNotification.dart:15:26)
E/flutter ( 8516): #5 _MyHomePageState.initState (package:flutter_app/main.dart:39:30)
E/flutter ( 8516): #6 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4812:57)
E/flutter ( 8516): #7 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4649:5)
E/flutter ( 8516): #8 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3615:14)
E/flutter ( 8516): #9 Element.updateChild (package:flutter/src/widgets/framework.dart:3380:18)
E/flutter ( 8516): #10 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6164:14)
E/flutter ( 8516): #11 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3615:14)
E/flutter ( 8516): #12 Element.updateChild (package:flutter/src/widgets/framework.dart:3380:18)
E/flutter ( 8516): #13 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4700:16)
E/flutter ( 8516): #14 Element.rebuild (package:flutter/src/widgets/framework.dart:4369:5)
E/flutter ( 8516): #15 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4654:5)
E/flutter ( 8516): #16 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4649:5)
E/flutter ( 8516): #17 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3615:14)
E/flutter ( 8516): #18 Element.updateChild (package:flutter/src/widgets/framework.dart:3380:18)
E/flutter ( 8516): #19 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6164:14)
E/flutter ( 8516): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3615:14)
E/flutter ( 8516): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:3380:18)
E/flutter ( 8516): #22 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:6164:14)
E/flutter ( 8516): #23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3615:14)
E/flutter ( 8516): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:3380:18)
E/flutter ( 8516): #25 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4700:16)
E/flutter ( 8516): #26 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4847:11)
E/flutter ( 8516): #27 Element.rebuild (package:flutter/src/widgets/framework.dart:4369:5)
E/flutter ( 8516): #28 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4654:5)
E/flutter ( 8516): #29 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4838:11)
E/flutter ( 8516): #30 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4649:5)
E/flutter ( 8516): #31 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3615:14)
E/flutter ( 8516): #32 Element.updateChild (package:flutter/src/widgets/framework.dart:3380:18)
E/flutter ( 8516): #33
I/flutter ( 8516): [SECRET TOKEN]
代码颤振面
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
},
onBackgroundMessage: myBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
//Static Function
Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) async {
print("################## ON BACKGROUND - APP IS RUNNING ON BACKGROUND ###################");
if (message.containsKey('data')) {
// Handle data message
final dynamic data = message['data'];
}
if (message.containsKey('notification')) {
// Handle notification message
final dynamic notification = message['notification'];
}
}
MainActivity.kt [app / src / kotlin / com / example / flutter_app / MainActivity.kt]
class Application : FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
override fun registerWith(registry: PluginRegistry?) {
io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
}
应用程序级别 bulid.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.google.gms.google-services'
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation platform('com.google.firebase:firebase-bom:25.12.0')
implementation 'com.google.firebase:firebase-analytics-ktx'
implementation 'com.google.firebase:firebase-messaging:20.3.0'
}
项目级别 bulid.gradle
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.4'
}
AndroidManifest.xml
<application
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
...
...
<intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
pubspec.yaml
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.0
firebase_messaging: ^7.0.3
** Flutter Doctor **
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel master, 1.24.0-2.0.pre.52, on Linux, locale en_IN)
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.0)
[✓] VS Code (version 1.49.1)
[✓] Connected device (3 available)
! Doctor found issues in 1 category.