J'ai intégré firebase_messaging v5.1.6 avec mon application flutter comme mentionné dans README avec l'option "gestion des messages d'arrière-plan").
Voici à quoi ressemblent mes fichiers.
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");
},
);
}
}
J'appelle FirebaseUtil().messagingInit()
dans initState
du widget d'application racine.
Voici le journal des erreurs que j'obtiens au démarrage de l'application.
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)
$ flutter doctor -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!
J'ai vérifié le README soigneusement et vérifié l'implémentation. Il me semble bien. Je ne sais pas ce qui ne va pas? Y a-t-il un bug dans le plugin?
Ce code exact fonctionne si je ne passe pas l'argument onBackgroundMessage
dans la méthode FirebaseMessaging.configure
.
L'un des plugins que vous utilisez (dans ce cas WifiIoTPlugin
) lève une exception nulle. Principalement, parce que le plugin est conçu uniquement pour le premier plan. firebase_messaging
est un plugin qui fonctionne également en arrière-plan, d'où le conflit.
Un problème similaire s'est produit lors de l'utilisation de Android_alarm_manager
et google_maps_flutter
ensemble comme Android_alarm_manager
.
Ce problème peut être rapidement résolu en n'enregistrant pas le plugin lorsqu'il n'y a pas d'activité. Vérifiez ceci Pull Request qui a été utilisé pour résoudre le problème mentionné ci-dessus.
Le plugin qui cause le code (plugin de premier plan uniquement) devrait ressembler à ceci:
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());
...
}
https://stackoverflow.com/a/55036042/7694194 Cela a fonctionné pour moi pour le crash de démarrage de l'application. Ajoutez tous les plugins que vous utilisez explicitement
Oui je pense que tu as raison. Je pense que cela fait partie de ce bug.
Le système de plug-in Android doit repenser la façon dont il fournit l'accès à Android composants # 22117
Pour plus de référence: https://github.com/flutter/flutter/issues/22117