重启/启动时Flutter FCM导航 [英] Flutter FCM Navigation onResume/onLaunch

查看:60
本文介绍了重启/启动时Flutter FCM导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我得到了一个家庭"类,在用户被授权后可以导航到该类.在主页"中,有一个带有BottomNavigationBar的PageView.当用户从Firebase(新消息)获得推送通知时,应在点击后将其导航到 ChatHome 和特定的聊天室.我该怎么办?

In my app, I got a class "home" where the user is navigated to after he is authorized. Within "home" there is a PageView with BottomNavigationBar. When the user is getting a push notification from firebase (new message) he should be navigated to ChatHome and to the specific chat after tapping. How can I do this?

房屋看起来像这样

@override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      body: PageView(
        children: <Widget>[
          Feed(userID: widget.userID),
          SearchView(),
          ChatHome(),
          Profile(
              uid: currentUser?.uid,
              auth: widget.auth,
              logoutCallback: widget.logoutCallback,
          ),
        ],
        controller: pageController,
        onPageChanged: onPageChanged,
        physics: NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: CupertinoTabBar(
          currentIndex: pageIndex,
          inactiveColor: Colors.white,
          backgroundColor: Color.fromRGBO(0, 111, 123, 1),
          activeColor: Color.fromRGBO(251, 174, 23, 1),
          onTap: onTap,
          items: [
            BottomNavigationBarItem(
              icon: Icon(Icons.home, size: 20),
              title: Text("Home"),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.search, size: 20),
              title: Text("Search"),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.chat, size: 20),
              title: Text("Chats"),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.profile, size: 20),
              title: Text("Profile"),
            ),
          ]),
    );
  }

在ChatHome中,我与所有聊天伙伴都获得了ListTiles.单击后,用户将导航到特定的聊天室.

In ChatHome I got ListTiles with all the chat partners. On click the user is navigated to the specific chat.

Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => Chat(
                    chatPartnerId: chatPartner[index].uid,
                    chatPartnerName: chatPartner[index].username,
                    chatPartnerPhotoUrl: chatPartner[index].photoUrl)));

在主页上,我得到了 onResume

我尝试了不同的方法,但是可以找到一个好的解决方案

I tried different things but could find a good solution

使用以下代码,我能够导航到ChatHome,但是BottomNavigationBar消失了.

With the following code, I was able to navigate to ChatHome but the BottomNavigationBar disappeared.

onResume: (Map<String, dynamic> message) async {
          final screen = message['screen'];
          print(screen);
          if(screen == "ChatHome"){
          Navigator.pushNamed(context, '/chatHome');
        } 
      },

以下代码无法正常运行,因为该应用程序跳来跳去总是在家里结束.

The following code didn't work well as the app jumped around and always ends in home.

onResume: (Map<String, dynamic> message) async {
          final screen = message['screen'];
          print(screen);
          if(screen == "ChatHome"){
          Navigator.pushNamed(context, '/home').then((_) => pageController.jumpToPage(2));
        } 
      },

routes: {
          '/rootPage': (BuildContext context) => new RootPage(auth: new Auth()),
          '/chatHome': (BuildContext context) => ChatHome(),
          '/home': (BuildContext context) => Home(),
        },


感谢您的帮助.

推荐答案

您需要为MaterialApp小部件提供一个navigatorKey:

You need to provide a navigatorKey to your MaterialApp Widget:

final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();

class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return MaterialApp(
            ....
            navigatorKey: navigatorKey,
            .....
        );
    }
}

然后,您可以使用此navigatorKey在 onResume 处理程序中进行导航:

Then, you can use this navigatorKey to navigate in your onResume handler:

onResume: (message) async {
    navigatorKey.currentState.pushNamed("/sample-route/" + message["data"]["..."])
}

对于onLaunch处理程序(直接称为onLaunch,意味着尚未构建第一个应用程序视图,这意味着您无法立即使用navigatorKey,我只找到了 hacky 解决方案:

For the onLaunch handler (which is called directly onLaunch, means the first app view isn't built yet which means you cannot use the navigatorKey instantly, I only found a hacky solution:

onLaunch: (message) async {
    Timer.periodic(
        Duration(milliseconds: 500),
        (timer) {
            if (navigatorKey.currentState == null) return;
            navigatorKey.currentState.pushNamed("/sample-route/" + message["data"]["..."]);
            timer.cancel();
        },
    );
}

当然,onLaunch的解决方案不是干净的,但是可以解决问题.

Of course, the solution for onLaunch isn't clean, but it works.

这篇关于重启/启动时Flutter FCM导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆