web-dev-qa-db-fra.com

La messagerie Flutter Firebase ne peut pas créer de service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

J'essaie d'utiliser la messagerie Firebase dans une application Flutter. Je suis toutes les instructions du paquet.

Dans mon pubspec.yaml, j'ai ces packages:

dependencies:
  flutter:
    sdk: flutter
  ...
  firebase_messaging: ^5.1.5
  ...

Mon main.app est juste ça:

void main() {
  runApp(MyApp());
}

MyApp est un StatefullWidget et à l'intérieur du _MyAppState, j'ai:

    FirebaseMessaging firebaseMessaging = FirebaseMessaging();
   @override
  void initState() {
    super.initState();
    NotificationHandler().initializeFcmNotification();
  }

Tout le code est dans whe notification_handler.Dart (merci à @HarishPenta):

...
import 'package:firebase_messaging/firebase_messaging.Dart';
...

class NotificationHandler {
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
  FirebaseMessaging _fcm = FirebaseMessaging();
  StreamSubscription iosSubscription;
  static final NotificationHandler _singleton =
  new NotificationHandler._internal();

  factory NotificationHandler() {
    return _singleton;
  }
  NotificationHandler._internal();

  Future<dynamic> myBackgroundMessageHandler(
      Map<String, dynamic> message) async {
    print("onLaunch: $message");
    // Or do other work.
  }

  initializeFcmNotification() async {
    flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();

    var initializationSettingsAndroid =
    new AndroidInitializationSettings('ic_launcher');
    var initializationSettingsIOS = new IOSInitializationSettings(
        onDidReceiveLocalNotification: onDidReceiveLocalNotification);
    var initializationSettings = new InitializationSettings(
        initializationSettingsAndroid, initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);

    if (Platform.isIOS) {
      iosSubscription = _fcm.onIosSettingsRegistered.listen((data) {
        // save the token  OR subscribe to a topic here
      });

      _fcm.requestNotificationPermissions(IosNotificationSettings());
    } else {
      _saveDeviceToken();
    }

    _fcm.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");
      },
    );
  }

...}

Dans mon AndroidManifest.xml, j'ai:

<activity
            Android:name=".MainActivity"
...
<intent-filter>
                <action Android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category Android:name="Android.intent.category.DEFAULT" />
            </intent-filter>

et, enfin, à l'intérieur de l'application/src/main/Java // Application.Java j'ai:

package <my ID app>;  --> the same ID in google-services.json

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.GeneratedPluginRegistrant;

import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;


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

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

Eh bien, l'application démarre OK et, au premier plan, lorsque j'envoie une notification, l'application s'arrête et je peux voir ce message d'erreur:

D/AndroidRuntime(26933): Shutting down VM
E/AndroidRuntime(26933): FATAL EXCEPTION: main
E/AndroidRuntime(26933): Process: br.com.htsnet.emergencia, PID: 26933
E/AndroidRuntime(26933): Java.lang.RuntimeException: Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: Java.lang.RuntimeException: PluginRegistrantCallback is not set.
E/AndroidRuntime(26933):    at Android.app.ActivityThread.handleCreateService(ActivityThread.Java:3167)
E/AndroidRuntime(26933):    at Android.app.ActivityThread.access$1900(ActivityThread.Java:177)
E/AndroidRuntime(26933):    at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1532)
E/AndroidRuntime(26933):    at Android.os.Handler.dispatchMessage(Handler.Java:102)
E/AndroidRuntime(26933):    at Android.os.Looper.loop(Looper.Java:145)
E/AndroidRuntime(26933):    at Android.app.ActivityThread.main(ActivityThread.Java:5951)
E/AndroidRuntime(26933):    at Java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(26933):    at Java.lang.reflect.Method.invoke(Method.Java:372)
E/AndroidRuntime(26933):    at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:1400)
E/AndroidRuntime(26933):    at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:1195)
E/AndroidRuntime(26933): Caused by: Java.lang.RuntimeException: PluginRegistrantCallback is not set.
E/AndroidRuntime(26933):    at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.Java:157)
E/AndroidRuntime(26933):    at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.onCreate(FlutterFirebaseMessagingService.Java:77)
E/AndroidRuntime(26933):    at Android.app.ActivityThread.handleCreateService(ActivityThread.Java:3157)
E/AndroidRuntime(26933):    ... 9 more

J'utilise Android Studio 3.5, Firebase Functions pour envoyer des notifications avec des données dans paypload (click_action: 'FLUTTER_NOTIFICATION_CLICK').

8
Hamilton

Suis confronté au même problème: Je viens de supprimer "Application.class" de Android. Le code fonctionne correctement.

0
Sathish Gadde