gpt4 book ai didi

android - Flutter Firebase Messaging 无法创建服务 io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

转载 作者:行者123 更新时间:2023-12-03 13:25:44 53 4
gpt4 key购买 nike

我正在尝试在 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();
}

所有代码都在 notify_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 发送通知,其中包含有效载荷中的数据(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;
}
}

主要 Activity
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);
}
}

关于android - Flutter Firebase Messaging 无法创建服务 io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58045161/

53 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com