【问题标题】:Firebase Messaging: Infinite onLaunch LoopFirebase 消息传递:无限的 onLaunch 循环
【发布时间】:2018-11-29 12:15:15
【问题描述】:

我已经使用 Flutter 构建了一个 Android 应用,并在该应用上集成了 Firebase 消息传递。

我遇到了一个问题,即如果 Android 设备在应用关闭时收到通知;该通知将无限启动onLaunch

我打开了一个问题github::flutter/flutter/issues/18524,但希望有人已经解决了这个问题(或知道原因)。

我尝试移动下面的代码,但仍然看到相同的结果。即使在设备上全新安装后,我仍然会看到问题。

以前有人遇到过这种情况吗?

@override
void initState() {
  super.initState();

  /// Navigate to item based on message type
  ///
  void _navigateToItemDetail(Map<String, dynamic> message) {
    if (message.containsKey("type")) {
      // handle messages by type
      String type = message["type"];
      String _id = message["_id"] ?? "";
      switch (type) {
        case "private_message":
          Application.router.navigateTo(context, "/private_message/$_id");
          break;
        case "announcement":
          FlutterWebBrowser.openWebPage(
            url: message["url"] ?? "https://me.app",
            androidToolbarColor: Colors.red
          );
          break;
        case "public_message":
          Application.router.navigateTo(context, "/public_message/$_id");
          break;
        default:
      }
    }
  }

  Future<Null> _showItemDialog(Map<String, dynamic> message, BuildContext ctx) async {
    return showDialog<Null>(
      context: ctx,
      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return new AlertDialog(
          title: new Text(
            'New Notification!',
            style: new TextStyle(
              fontWeight: FontWeight.w800,
            )
          ),
          content: new SingleChildScrollView(
            child: new ListBody(
              children: <Widget>[
                new Text(message["summary"] ?? "You have a message"),
              ],
            ),
          ),
          actions: <Widget>[
            new FlatButton(
              textColor: Colors.red[900],
              child: new Text(
                "View",
                style: new TextStyle(fontFamily: "Roboto")
              ),
              onPressed: () {
                _navigateToItemDetail(message);
                Navigator.pop(context);
              }
            ),
            new FlatButton(
              textColor: Colors.red[900],
              child: new Text('Ignore', style: new TextStyle(fontFamily: "Roboto")),
              onPressed: () {
                Navigator.pop(context);
              },
            ),
          ],
        );
      },
    ).then((shs) {
      print("$shs results");
    });
  }

  _firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) {
      print("onMessage: $message");
      _showItemDialog(message, context);
    },
    onLaunch: (Map<String, dynamic> message) {
      print("onLaunch: $message");
      _navigateToItemDetail(message);
    },
    onResume: (Map<String, dynamic> message) {
      print("onResume: $message");
      _navigateToItemDetail(message);
    },
  );
}

【问题讨论】:

  • 在 GitHub 上回答。
  • 事实证明,我正在导航到我设置 Firebase 配置的同一个小部件。 @ArthurThompson 引导我朝着正确的方向前进,现在问题已经解决了!

标签: android firebase-cloud-messaging flutter


【解决方案1】:

问题不在于 Firebase 消息传递。

对于发现自己在这里的其他人:

我有一个BottomNavigation,它基于 Flutter 中的演示。我正在导航到同一个 BottomNavigation,这导致了无限导航循环。

【讨论】:

    猜你喜欢
    • 2020-06-21
    • 2021-03-31
    • 2021-04-18
    • 2014-07-17
    • 2020-05-05
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多