锁屏Flutter上的FCM通知

FCM notification on locked screen Flutter

我开发了一个应用程序,因为当 phone 被锁定时我无法收到 FCM 通知。

_fcm.configure(
  onMessage: (Map<String, dynamic> message) async {
    print("onMessage: $message");
    AwesomeNotifications().createNotification(
        content: NotificationContent(
            id: 100,
            channelKey: "basic_channel",
            title: message['notification']['title'],
            body: message['notification']['body'],
            showWhen: true,
            autoCancel: true));
  },
  onLaunch: (Map<String, dynamic> message) async {
    print("onLaunch: $message");
  },
  onResume: (Map<String, dynamic> message) async {
    print("onResume: $message");
  },
);

我使用了 firebase 和 Awesome Notification 插件来显示通知。

使用的包:firebase_messaging:^7.0.3

下面是我展示的代码

   class _MyAppState extends State<MyApp> {
  @override
    Widget build(BuildContext context) {
      final pushNotificationService = PushNotificationService(_firebaseMessaging);
      pushNotificationService.initialise();
  }
}

谁能帮帮我,我卡在这里了 谢谢

我可以建议你改变功能,你可以这样实现:

void subscribeChannel(String data) async =>
    await FirebaseMessaging.instance.subscribeToTopic(data);

void unsubscribeChannel(String channelId) async =>
    await FirebaseMessaging.instance.unsubscribeFromTopic(channelId);

void listenNotificationEvents() {
  FirebaseMessaging.instance.getInitialMessage().then(
    (value) async {
      if (value != null) setLocalNotification(value);
    },
  );
  FirebaseMessaging.onMessage.listen((event) {
    if (event.data.isNotEmpty) setLocalNotification(event);
  });
  FirebaseMessaging.onMessageOpenedApp.listen((event) {
    if (event.data.isNotEmpty) changeScreen(event.data);
  });
}

这些功能基本上是让 firebase 设置所有内容并启用即使应用已被终止或在后台也能收到通知。

在您的 MaterialApp class 上,您需要使用它。

FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
try {
  FirebaseMessaging.onBackgroundMessage(
      (message) => onBackgroundMessage(message.data));
} catch (_) {}

最后,如果您收到通知,并且应用程序已打开,firebase 将执行块代码中的所有内容,因此最后的更改是在 onMessage 中设置本地通知的设置。

如果对你有帮助,请告诉我。

在我的情况下,通知已收到(我可以听到声音)但未显示在锁定屏幕上。 在应用程序设置中,锁屏通知显示是默认关闭的,我启用了它,一切正常。