【问题标题】:onBackgroundMessage is triggered but don't add event to my list ( Flutter, FCM )onBackgroundMessage 已触发但不将事件添加到我的列表中(Flutter、FCM)
【发布时间】:2022-11-23 20:30:44
【问题描述】:

我有一个在后台触发的 FCM 的 onBackgroundMessage 函数

Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  if (kDebugMode) {
    print('Handling a background message ${message.messageId}');
    print('message: ${message.data}');
  }
  GlobalStream.addBackgroundMessage(message);
}

addBackground 消息假设将来自 FCM 的消息事件添加到列表中,以便在应用程序返回前台时重新发送

static final _onBcakgroundMessages = <dynamic>[];

static void addBackgroundMessage(dynamic data) {
  log('Adding background message to background messages');
  _onBcakgroundMessages.add(data);
}

它们似乎都被触发了,但是当应用程序回到前台时,列表是空的。

在 FCM 文档中,我读到我可以在后台更新数据,并认为可以更新一个简单的列表。

可以用这样的列表来完成还是需要我将它们存储在数据库或类似的东西中?

感谢任何可以帮助我的人!

【问题讨论】:

    标签: flutter firebase-cloud-messaging


    【解决方案1】:

    当您的应用程序在页面的 onInit() 函数中从后台转到前台时,您必须使用 FirebaseMessaging.instance.getInitialMessage() 函数。然后从此函数检索 RemoteMessage,然后您可以运行您的函数GlobalStream.addBackgroundMessage(message);

    假设您的用户已从最近的应用程序中终止/终止了该应用程序。然后您向您的用户发送通知。当用户收到通知后,如果用户点击通知打开应用程序,将触发FirebaseMessaging.instance.getInitialMessage(),然后您可以运行任何您喜欢的功能。

    【讨论】:

      猜你喜欢
      • 2020-06-02
      • 2021-03-08
      • 2011-08-10
      • 2014-10-04
      • 2010-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      相关资源
      最近更新 更多