【问题标题】:Flutter Push Notification onLaunch call infinitelyFlutter Push Notification onLaunch 无限调用
【发布时间】:2019-09-10 10:39:36
【问题描述】:

如果有通知,我将创建一个推送导航到 ChatScreen。 它在推动,但问题是它在无限推动。

我将它用于 Firebase 消息传递

_firebaseMessaging.configure(
      //Display when the apps on close
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch : $message\n");
        if (message['data']['type'] == 'like') {
          print("Navigate to Notification");
          print("type : ${message['data']['type']}");
        } else if (message['data']['type'] == 'chat') {
          print("userID : ${message['data']['userID']}");
          print("sellerID : ${message['data']['sellerID']}");
          print("type : ${message['data']['type']}");
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => ChatScreen(
                userID: message['data']['userID'],
                sellerID: message['data']['sellerID'],
                productName: "",
                firstMessage: "",
                price: 0,
                imageUrls: [""],
              ),
            ),
          );
        }
      },
      //Display when the apps on background
      onResume: (Map<String, dynamic> message) async {
        print("onResume : $message\n");
        if (message['data']['type'] == 'like') {
          print("Navigate to Notification");
          print("type : ${message['data']['type']}");
        } else if (message['data']['type'] == 'chat') {
          print("userID : ${message['data']['userID']}");
          print("sellerID : ${message['data']['sellerID']}");
          print("type : ${message['data']['type']}");
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => ChatScreen(
                userID: message['data']['userID'],
                sellerID: message['data']['sellerID'],
                productName: "",
                firstMessage: "",
                price: 0,
                imageUrls: [""],
              ),
            ),
          );
        }
      },
      //Display when the apps is active
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage : $message\n");

      },
    );

这就是应用程序上发生的事情

这是终端上的输出

I/flutter (13793): onLaunch : {data: {google.sent_time: 1568110868915, click_action: FLUTTER_NOTIFICATION_CLICK, userID: IGgqN2pqzKPk6pNN7Sl3NT8mois2, google.original_priority: high, status: done, collapse_key: id.co.sprout.etalase, sellerID: VduOp2mICGcLjQDXFXJVRrkmV6n2, groupChatID: IGgqN2pqzKPk6pNN7Sl3NT8mois2-VduOp2mICGcLjQDXFXJVRrkmV6n2, google.delivered_priority: high, sound: default, from: 11013836573, type: chat, google.message_id: 0:1568110868934877%c5168753c5168753, google.ttl: 2419200}, notification: {}}
I/flutter (13793): userID : IGgqN2pqzKPk6pNN7Sl3NT8mois2
I/flutter (13793): sellerID : VduOp2mICGcLjQDXFXJVRrkmV6n2
I/flutter (13793): type : chat
I/flutter (13793): Token: cOZlHYsGTKk:APA91bFsYC1DBR-Ysij14IwGienYN9Prq5JiBsvVBaVtthKAb0ZCfOxGW4cKU95IQqA3zw-BkunLe8G5wB4FLFvYDPDj_WvRLY377yNU2pX5R0N6C3vMg5xHCSfhmzJtF_16Yp5ovxfg

而且是无限循环

【问题讨论】:

  • 确保您的 firebase 代码不在 build 方法中,因为 build 方法中的任何内容都会被多次调用,例如每当布局更改时。
  • 该函数已经在Screen Build之外,代码在initState下面,但是在didChangeDependencies上调用了

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


【解决方案1】:

我回答了我自己的问题

我把它放在 didChangeDependencies 上,所以它一次又一次地调用

现在我把它放在initState上,然后解决问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-10
    • 1970-01-01
    • 2019-06-19
    • 2020-06-03
    • 2020-04-22
    • 1970-01-01
    • 2018-12-16
    相关资源
    最近更新 更多