如果处理后台消息,firebase_messaging 颤振插件在启动时崩溃
Posted
技术标签:
【中文标题】如果处理后台消息,firebase_messaging 颤振插件在启动时崩溃【英文标题】:firebase_messaging flutter plugin crashing on startup if handling background messages 【发布时间】:2020-01-29 07:39:55 【问题描述】:我将 firebase_messaging v5.1.6 与我在自述文件中提到的 Flutter 应用集成了“处理后台消息”选项。
这是我的文件的外观。
MyApplication.kt
class MyApplication : FlutterApplication(), PluginRegistrantCallback
override fun onCreate()
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this)
override fun registerWith(registry: PluginRegistry)
GeneratedPluginRegistrant.registerWith(registry)
firebase_util.dart
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_analytics/observer.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
final kFirebaseAnalyticsObserver =
FirebaseAnalyticsObserver(analytics: FirebaseUtil().analytics);
Future<dynamic> kFirebaseMessagingBackgroundMessageHandler(
Map<String, dynamic> message) async
if (message.containsKey('data'))
// Handle data message
final dynamic data = message['data'];
if (message.containsKey('notification'))
// Handle notification message
final dynamic notification = message['notification'];
class FirebaseUtil
static final FirebaseUtil _firebaseUtil = FirebaseUtil._internal();
factory FirebaseUtil()
return _firebaseUtil;
FirebaseUtil._internal();
final analytics = FirebaseAnalytics();
final messaging = FirebaseMessaging();
void messagingInit()
messaging.requestNotificationPermissions();
messaging.configure(
onMessage: (Map<String, dynamic> message) async
print("onMessage: $message");
,
onBackgroundMessage: kFirebaseMessagingBackgroundMessageHandler,
onLaunch: (Map<String, dynamic> message) async
print("onLaunch: $message");
,
onResume: (Map<String, dynamic> message) async
print("onResume: $message");
,
);
我在根 App 小部件的 initState
中调用 FirebaseUtil().messagingInit()
。
这是我在应用启动时收到的错误日志。
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): Failed to handle method call
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.Activity.getApplicationContext()' on a null object reference
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at com.alternadom.wifiiot.WifiIotPlugin.<init>(WifiIotPlugin.java:65)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at com.alternadom.wifiiot.WifiIotPlugin.registerWith(WifiIotPlugin.java:76)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:40)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at <<package_name>>.MyApplication.registerWith(MyApplication.kt:18)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:164)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.onMethodCall(FirebaseMessagingPlugin.java:134)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:656)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at android.os.MessageQueue.next(MessageQueue.java:326)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at android.os.Looper.loop(Looper.java:160)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at android.app.ActivityThread.main(ActivityThread.java:6669)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/MethodChannel#plugins.flutter.io/firebase_messaging( 6252): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
...
E/flutter ( 6252): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'android.content.Context android.app.Activity.getApplicationContext()' on a null object reference, null)
E/flutter ( 6252): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter ( 6252): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
E/flutter ( 6252): <asynchronous suspension>
E/flutter ( 6252): #2 FirebaseMessaging.configure (package:firebase_messaging/firebase_messaging.dart:118:16)
E/flutter ( 6252): #3 FirebaseUtil.messagingInit (package:<<package_name>>/util/firebase_util.dart:37:15)
E/flutter ( 6252): #4 _MainPageState.initState (package:<<package_name>>/pages/main.dart:29:20)
E/flutter ( 6252): #5 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4068:58)
E/flutter ( 6252): #6 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3919:5)
E/flutter ( 6252): #7 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3101:14)
E/flutter ( 6252): #8 Element.updateChild (package:flutter/src/widgets/framework.dart:2904:12)
E/flutter ( 6252): #9 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3961:16)
E/flutter ( 6252): #10 Element.rebuild (package:flutter/src/widgets/framework.dart:3738:5)
E/flutter ( 6252): #11 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2348:33)
E/flutter ( 6252): #12 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:760:20)
E/flutter ( 6252): #13 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:280:5)
E/flutter ( 6252): #14 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1033:15)
E/flutter ( 6252): #15 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:975:9)
E/flutter ( 6252): #16 SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:891:5)
E/flutter ( 6252): #17 _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 6252): #18 _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 6252): #19 _CustomZone.runGuarded (dart:async/zone.dart:923:7)
E/flutter ( 6252): #20 _invoke (dart:ui/hooks.dart:249:10)
E/flutter ( 6252): #21 _drawFrame (dart:ui/hooks.dart:207:3)
$颤振医生-v
[✓] Flutter (Channel stable, v1.9.1+hotfix.2, on Linux, locale en_IN)
• Flutter version 1.9.1+hotfix.2 at /home/harsh/flutter
• Framework revision 2d2a1ffec9 (3 weeks ago), 2019-09-06 18:39:49 -0700
• Engine revision b863200c37
• Dart version 2.5.0
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at /home/harsh/Android/Sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.3
• Java binary at: /home/harsh/android-studio/jre/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
• All Android licenses accepted.
[✓] Android Studio (version 3.5)
• Android Studio at /home/harsh/android-studio
• Flutter plugin version 39.0.3
• Dart plugin version 191.8423
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
[✓] Connected device (1 available)
• Android SDK built for x86 • emulator-5554 • android-x86 • Android 9 (API 28) (emulator)
• No issues found!
我仔细检查了自述文件并检查了实现。对我来说看起来不错。不知道有什么问题?插件有BUG吗?
如果我没有在 FirebaseMessaging.configure
方法中传递 onBackgroundMessage
参数,则此确切代码有效。
【问题讨论】:
【参考方案1】:https://***.com/a/55036042/7694194 这对我来说适用于应用程序启动崩溃。明确添加您正在使用的所有插件
【讨论】:
这是一个很好的解决方法,但问题在于您正在使用的第 3 方插件,因此您应该向插件维护者报告此问题并告诉他修复它。【参考方案2】:您正在使用的插件之一(在本例中为 WifiIoTPlugin
)正在引发空异常。大多数情况下,因为该插件是为仅前台场景而设计的。 firebase_messaging
是一个也在后台工作的插件,因此这就是冲突。
将android_alarm_manager
和google_maps_flutter
一起用作android_alarm_manager
时会导致类似的issue。
这个问题可以通过在没有活动时不注册插件来快速解决。检查用于解决上述问题的Pull Request。
导致问题的插件(仅限前台插件)代码应如下所示:
public static void registerWith(Registrar registrar)
if (registrar.activity() == null)
// When a background flutter view tries to register the plugin, the registrar has no activity.
// We stop the registration process as this plugin is foreground only.
return;
final ForegroundPlugin foregroundPlugin = new ForegroundPlugin(registrar.activity());
...
【讨论】:
我面临同样的问题,它在启动时崩溃,当我删除自定义应用程序类时崩溃消失了,但我需要这个后台消息才能工作 或许,我应该用新版本的 Flutter 使用新的 android 文件夹?【参考方案3】:是的,我认为你是对的。我认为这是这个错误的一部分。
Android 插件系统需要重新构想它如何提供对 Android 组件的访问 #22117
更多参考: https://github.com/flutter/flutter/issues/22117
【讨论】:
那他们为什么要把这部分放在插件里呢?我不明白为什么它对我不起作用?是因为其他插件吗? 我正在使用的WifiIoTPlugin
存在问题 - github.com/alternadom/WiFiFlutter/issues/45以上是关于如果处理后台消息,firebase_messaging 颤振插件在启动时崩溃的主要内容,如果未能解决你的问题,请参考以下文章
[Firebase 消息传递]:应用程序在后台时不调用后台消息处理程序方法?