【问题标题】:FCM notification on locked screen Flutter锁定屏幕上的 FCM 通知 Flutter
【发布时间】:2021-07-21 23:19:25
【问题描述】:

我开发了一款应用,手机锁定时无法收到 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();
  }
}

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

【问题讨论】:

  • 您可以尝试将优先级设置为高,看看是否有帮助
  • 发布适当的代码,例如用于 FCM 的库、已初始化 fcm 接收器的类等
  • @Quicklearner 我更新了问题,如果有帮助请告诉我

标签: android flutter push-notification firebase-cloud-messaging


【解决方案1】:

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

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 中设置本地通知的设置。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      • 1970-01-01
      • 2023-01-25
      • 2020-04-06
      相关资源
      最近更新 更多