Flutter Firebase Messaging 无法创建服务 io.flutter.plugins.fireba



我正在尝试在 Flutter 应用中使用 Firebase 消息传递。我遵循包装中的所有说明。

在我的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");
},
);
}
...}

在我的 Android 清单中.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"(。

将下面提到的文件添加到您的 android/app/main/java/com/

应用.java

package com.example.<application Name>;
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
public class Application extends FlutterApplication implements PluginRegistrantCallback {
@Override
public void onCreate() {
super.onCreate();
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
@Override
public void registerWith(PluginRegistry registry) {
FirebaseCloudMessagingPluginRegistrant.registerWith(registry);
}
}

FirebaseCloudMessagingPluginRegistrant

package com.example.<application Name>;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;
public final class FirebaseCloudMessagingPluginRegistrant{
public static void registerWith(PluginRegistry registry) {
if (alreadyRegisteredWith(registry)) {
return;
}
FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
private static boolean alreadyRegisteredWith(PluginRegistry registry) {
final String key = FirebaseCloudMessagingPluginRegistrant.class.getCanonicalName();
if (registry.hasPlugin(key)) {
return true;
}
registry.registrarFor(key);
return false;
}
}

主活动

package com.example.<application Name>;
import android.os.Bundle;
import  `io.flutter.app.FlutterActivity`;
import `io.flutter.plugins.GeneratedPluginRegistrant`;
public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
}
}

正在工作'

import io.flutter.app.FlutterApplication;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
public class Application extends FlutterApplication implements PluginRegistrantCallback {
@Override
public void onCreate() {
super.onCreate();
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
@Override
public void registerWith(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith((FlutterEngine) registry);
}
}

//这对我有用

import io.flutter.app.FlutterApplication;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingBackgroundService;
public class Application extends FlutterApplication implements PluginRegistrantCallback {
// ...
@Override
public void onCreate() {
super.onCreate();
FlutterFirebaseMessagingBackgroundService.setPluginRegistrant(this);
}
@Override
public void registerWith(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith((FlutterEngine) registry);
}
// ...
}

在您的清单中确保更改

android:name="io.flutter.app.FlutterApplication"

android:name=".Application"

我遇到了同样的问题:刚刚从Android项目中删除了"应用程序.class"。代码工作正常。

相关内容

  • 没有找到相关文章

最新更新