Flutter 中未设置 PluginRegistrantCallback

Ema*_*per 7 flutter firebase-cloud-messaging

当我运行我的应用程序时出现此错误,并且应用程序关闭时的后台通知不起作用:

firebase_messaging: ^7.0.3

final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
  

  @override
  void initState() {
    super.initState();
    registerNotification();
  }



  void registerNotification() {
    firebaseMessaging.requestNotificationPermissions();

    firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
      print('onMessage: $message');
      return ;
    }, onResume: (Map<String, dynamic> message) {
      print('onResume: $message');
      return Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => NotificationsScreen()));
    }, onBackgroundMessage: _firebaseMessagingBackgroundHandler,
        onLaunch: (Map<String, dynamic> message) {
      print('onLaunch: $message');
      return;
    });

    firebaseMessaging.getToken().then((token) {
      print('token: $token');
      FirebaseFirestore.instance
          .collection('Consultant')
          .doc(firebaseUser.uid)
          .update({'deviceToken': token});
    }).catchError((err) {
      //Fluttertoast.showToast(msg: err.message.toString());
    });
  }
Run Code Online (Sandbox Code Playgroud)

从课堂上出来:

Future<dynamic> _firebaseMessagingBackgroundHandler(
    Map<String, dynamic> message,
    ) async {
  // Initialize the Firebase app
  await Firebase.initializeApp();
  print('onBackgroundMessage received: $message');
}
Run Code Online (Sandbox Code Playgroud)

我有 :ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(error, PluginRegistrantCallback is not set., null, java.lang.RuntimeException: PluginRegistrantCallback is not set.

我发现的问题是当应用程序关闭时我无法收到通知。在互联网上搜索时我看到:PluginRegistrantCallback is not set is an error which attempts to put this file without successing. 有人有什么建议吗?

yah*_*007 2

该错误来自您的应用程序文件。确保正确注册。

应用程序.java

package <your package name>;

import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;
public final class CustomPluginRegistrant {
    public static void registerWith(PluginRegistry registry) {
        if (alreadyRegisteredWith(registry)) {
            return;
        }
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"));
    }

    private static boolean alreadyRegisteredWith(PluginRegistry registry) {
        final String key = CustomPluginRegistrant.class.getCanonicalName();
        if (registry.hasPlugin(key)) {
            return true;
        }
        registry.registrarFor(key);
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

在同一路径中添加一个名为 CustomPluginRegistant.java 的文件

package <your package name>;

import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;
public final class CustomPluginRegistrant {
    public static void registerWith(PluginRegistry registry) {
        if (alreadyRegisteredWith(registry)) {
            return;
        }
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"));
    }

    private static boolean alreadyRegisteredWith(PluginRegistry registry) {
        final String key = CustomPluginRegistrant.class.getCanonicalName();
        if (registry.hasPlugin(key)) {
            return true;
        }
        registry.registrarFor(key);
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您不使用本地通知插件,则可以从代码中删除该部分。

  • 为什么Application.java中的代码与CustomPluginRegistrant中的代码相似? (2认同)