web-dev-qa-db-ja.com

Flutter Firebaseメッセージングでサービスio.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingServiceを作成できません

FlutterアプリでFirebase Messagingを使用しようとしています。私はパッケージからのすべての指示に従います。

私のpubspec.yamlには次のパッケージがあります。

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

私のmain.appはそれだけです:

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

MyAppはStatefullWidgetであり、_MyAppState内にあります。

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

すべてのコードはnotification_handler.Dart内にあります(@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");
      },
    );
  }

...}

私のAndroidManifest.xmlには次のものがあります:

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

そして最後に、app/src/main/Java // Application.Java内に次のように記述します。

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);
    }
}

アプリは正常に起動し、フォアグラウンドで通知を送信するとアプリが停止し、次のエラーメッセージが表示されます。

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

Android Studio 3.5、Firebase Functionsを使用して、paypload(click_action: 'FLUTTER_NOTIFICATION_CLICK')でデータ付きの通知を送信しています。

8
Hamilton

同じ問題に直面しています:Androidプロジェクトから "Application.class"を削除しました。コードは正常に動作しています。

0
Sathish Gadde