【问题标题】:onBackgroundMessage open specified pageonBackgroundMessage 打开指定页面
【发布时间】:2020-03-12 08:40:01
【问题描述】:

所以,我正在尝试处理 onBackgroundMessage。我已经能够处理 onMessage、onResume 和 onLaunch。

这里是我的myBackgroundMessageHandler 来处理onBackgroundMessage

static Future<dynamic> myBackgroundMessageHandler(
      Map<String, dynamic> message) {}

我想在我的myBackgroundMessageHandler 打开指定页面。在onResume 我可以做到这一点

   Data data = Data(
          clickAction: message['data']['click_action'],
          sound: message['data']['sound'],
          status: message['data']['status'],
          screen: message['data']['screen'],
          extradata: message['data']['extradata'],
        );

        if (data.screen == "MAINTENANCE_DETAIL") {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => new MaintenanceDetail(
                RequestMaintenanceCode: data.extradata,
              ),
            ),
          );
        }

但是,当我将其复制到我的 myBackgroundMessageHandler 时。我收到此错误

Error: Getter not found: 'context'.

我认为错误是因为myBackgroundMessageHandler 是静态方法,所以我如何在myBackgroundMessageHandler 中打开屏幕?

【问题讨论】:

  • 应用程序在调用该方法时处于后台,因此它无法导航到任何地方。仅当应用程序处于前台时才可以进行导航。您应该能够做的是保留信息(例如在共享首选项中),并在应用程序恢复时读取该信息,并基于该信息进行导航。
  • 你找到解决办法了吗?

标签: flutter dart firebase-cloud-messaging


【解决方案1】:

onBackgroundMessage 在后台工作,因此您无法导航到您的路线。最好的方法是在您的基类上,查看initState() 上的通知。这有助于立即导航路线。我的意思是,在构建方法运行之前检查通知并导航。没有其他办法了。

  @override
  void initState() {
    super.initState();
    Future.microtask(() {
      Provider.of<BaseViewBloc>(context).initFCM(); // This line check the notifications.
  });

这些行有助于导航:

  Future<void> onResume(Map<String, dynamic> message, context) async {
    print('onResume $message');
    var data = message["data"];
    Navigator.pushNamed(context, NotificationContentPage.routeName,
        arguments: int.parse(data["id"]));
  }

  Future<void> onLaunch(Map<String, dynamic> message, context) async {
    print('on launch $message');
    var data = message["data"];
    Navigator.pushNamed(context, NotificationContentPage.routeName,
        arguments: int.parse(data["id"]));
  }

【讨论】:

    【解决方案2】:

    当用户单击您的通知时,应用程序将打开并调用以下方法。因此,在 fcm init 方法中传递上下文,您可以轻松导航到适用的屏幕。

    // onBackgroundMessage: myBackgroundMessageHandler,
      onLaunch: (Map<String, dynamic> message) async {
        if (message['data']['notification_type'] == 'sell') {
          //Navigate now
        }
      },
    

    【讨论】:

      猜你喜欢
      • 2010-12-08
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 2012-04-30
      • 1970-01-01
      • 2015-09-26
      • 2021-04-05
      • 2014-08-29
      相关资源
      最近更新 更多