【问题标题】:Flutter Firebase Cloud Messaging: for what is the `onBackgroundMessage` used for?Flutter Firebase 云消息传递:`onBackgroundMessage` 的用途是什么?
【发布时间】:2021-09-06 16:04:42
【问题描述】:

我使用推送通知设置了我的颤振应用程序,但我不知道以下代码用于什么:

  _initiateFcm() async {
      FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
  }

  Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
    await Firebase.initializeApp();
    print('A bg message just showed up: ${message.messageId}');
  }

除了初始化 firebase 应用程序之外,我没有找到将它用于其他任何用途的示例……在我的 main() 方法中已经发生了什么。这样做的目的是什么?什么时候用?

我在实际处理收到的推送通知时拥有这些功能:

FirebaseMessaging.onMessage.listen((RemoteMessage message) {});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {});

但我不会将onBackgroundMessage 用于任何事情...

文档说:

  /// Set a message handler function which is called when the app is in the
  /// background or terminated.
  ///
  /// This provided handler must be a top-level function and cannot be
  /// anonymous otherwise an [ArgumentError] will be thrown.

但是没有人在我检查的任何地方使用这种方法......

【问题讨论】:

    标签: firebase flutter dart push-notification


    【解决方案1】:
    FirebaseMessaging.onBackgroundMessage()
    

    当应用程序未运行(应用程序处于后台)时收到您的通知时调用此函数。

    所以,当收到后台通知时,如果你有想做的事情,你可以使用这个功能。

    您可以使用该功能执行诸如 HTTP 请求、IO 操作(更新本地存储)等逻辑,当您在应用未运行时收到通知时与其他插件通信。

    注意:无法通过此函数更新应用程序状态或执行任何影响 UI 的逻辑(因为处理程序在应用程序上下文之外的自己的隔离中运行)。

    更多info

    【讨论】:

      猜你喜欢
      • 2017-04-21
      • 1970-01-01
      • 2020-07-09
      • 2021-07-22
      • 2021-01-29
      • 1970-01-01
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多