Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter onBackgroundMessage打开指定页面_Flutter_Dart_Firebase Cloud Messaging - Fatal编程技术网

Flutter onBackgroundMessage打开指定页面

Flutter onBackgroundMessage打开指定页面,flutter,dart,firebase-cloud-messaging,Flutter,Dart,Firebase Cloud Messaging,所以,我正在尝试处理背景信息。我已经能够处理onMessage、onResume和onLaunch 这里我的myBackgroundMessageHandler处理onBackgroundMessage 但是,当我把它复制到我的myBackgroundMessageHandler时。我得到这个错误 Error: Getter not found: 'context'. 我认为错误是因为myBackgroundMessageHandler是静态方法,所以如何打开myBackgroundMessa

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

这里我的myBackgroundMessageHandler处理onBackgroundMessage

但是,当我把它复制到我的myBackgroundMessageHandler时。我得到这个错误

Error: Getter not found: 'context'.

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

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

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

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

// onBackgroundMessage: myBackgroundMessageHandler,
  onLaunch: (Map<String, dynamic> message) async {
    if (message['data']['notification_type'] == 'sell') {
      //Navigate now
    }
  },
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"]));
  }
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"]));
  }

该应用程序在调用该方法时处于后台,因此无法在任何地方导航。只有当应用程序位于前台时,才可以进行导航。您应该能够做的是将信息持久化(例如,在共享首选项中),并在应用程序恢复时读取该信息,然后根据该信息进行导航。您找到解决方案了吗?应用程序在调用该方法时处于后台,因此无法在任何位置进行导航。只有当应用程序位于前台时,才可以进行导航。您应该能够做的是将信息持久化(例如在共享首选项中),并在应用程序恢复时读取这些信息,然后根据这些信息进行导航。您找到解决方案了吗?