首页 > 解决方案 > 当应用程序通过使用 firebase_messaging 的推送通知终止时如何接收自定义数据?

问题描述

如标题所示,当用户在应用程序终止时单击通知时接收自定义数据的当前解决方法是什么?

似乎在 Android上无法在 onLaunch 中接收数据消息(这是理想的方式)在 IOS 上我还没有尝试过,因为我首先遇到了这个问题。

有什么线索吗?

附加信息:我通过 a 发送的通知firebase cloud function采用以下形式:

  "message":{
    "token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
    "notification":{
      "title":"Portugal vs. Denmark",
      "body":"great match!"
    },
    "data" : {
      "Nick" : "Mario",
      "Room" : "PortugalVSDenmark"
    }
  }
}

https://firebase.google.com/docs/cloud-messaging/concept-options

onResume我能够执行一个动作,但onLaunch似乎没有被调用。

标签: flutterfirebase-cloud-messaging

解决方案


我认为,您已经在启动时获得了数据,只需要稍等一下。我写它是因为我遇到了同样的事情。

你可以试试这个,

firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async { .. },
  onResume: (Map<String, dynamic> message) async { .. },
  onLaunch: (Map<String, dynamic> message) async {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      Timer(const Duration(seconds: 7), () {
        // your process...
      });
    });
  },
);

同样在我的情况下,我就这样使用它并解决了。

onLaunch: (Map<String, dynamic> message) async {
    WidgetsBinding.instance.addPostFrameCallback((ignored) {
      Timer.periodic(const Duration(seconds: 1), (timer) {
        if (CurrentUser.currentUser != null) {
          timer.cancel(); // important **
          // redirect process with post details inside data
          if (message['data']['type'] == NotifTypes.NEW_POST)
            goGroupAndPost(Map.from(message['data'])); 
        }
      });
    });
  },

推荐阅读