如何从推送通知重定向到 flutter 中的页面

use*_*135 5 dart flutter

我正在我的 flutter 应用程序中实现推送通知,需要一些有关如何重定向的帮助。

基本上,这是正常流程

应用程序->根->主页->列表->详细信息。有默认的后退导航

当用户收到推送通知并单击它时,他们会直接进入详细信息页面。这是正确的,但我想看到一个后退按钮,可以让他们转到主屏幕。

应用程序->详细信息

我想要的是用户查看详细信息后他们应该返回主屏幕。但是,在这种情况下没有后退按钮

这是我的根页面

class RootPage extends StatefulWidget {

  RootPage({Key key}) : super(key: key);
  _RootPage createState() => _RootPage();
}
class _RootPage extends State<RootPage> with WidgetsBindingObserver {
  AppLifecycleState _appLifecycleState;
  var _message;
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    setState(() {
      _appLifecycleState = state;
    });
  }
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) {
        setState(() {
          _message = message;
        });
      },
      //app in background
      onResume: (Map<String, dynamic> message) {
        setState(() {
          _message = message;
        });
      },
    );
    _firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(sound: true, badge: true, alert: true));
  }
  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {

    if (_message != null) {
      if (_appLifecycleState != null && _appLifecycleState.index == 0) {
        // app is in resume state
        if (_message["type"] == "Notification") {
          String _Id = _message["Id"];
          String _reference = _message["reference"];
          return (DetailPage(Id: _Id, reference: _reference));
        }
      }
    } //end if
    return MyHomePage();
  }
}
Run Code Online (Sandbox Code Playgroud)

主页

class MyHomePage extends StatefulWidget {

  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {

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

  }
  final List<Widget> _children = [
    List(),
    Search(),
    MyCalendarPage(),
  ];
  var _currentIndex = 0;
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
          appBar: AppBar(
            title: Center(child: Text("my app")),
            actions: <Widget>[
              new IconButton(
                icon: new Icon(Icons.favorite),
                onPressed: () {},
              ),

            ],
          ),
          drawer: Drawer(),
          body: _children[_currentIndex],
          bottomNavigationBar: BottomNavigationBar(
              type: BottomNavigationBarType.fixed,
              onTap: onTabTapped, // new
              currentIndex: _currentIndex, // new
              items: [
                new BottomNavigationBarItem(
                  icon: new Icon(Icons.home),
                  title: new Text("home"),
                ),
                new BottomNavigationBarItem(
                  icon: new Icon(Icons.search),
                  title: new Text("search"),
                ),
                new BottomNavigationBarItem(
                  icon: new Icon(Icons.schedule),
                  title: new Text("schedule"),
                )
              ]
              )
              );
  }

  void onTabTapped(int index) {
    setState(() {
      _currentIndex = index;
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

如果有推送通知,如何转到主屏幕然后重定向到详细信息页面?我相信那时我会看到一个后退按钮,因为上一页将是主屏幕。

不知道如何重新编写我的代码来实现这一点

谢谢您的帮助

ZeR*_*eRj 1

如果您想要后退按钮进行导航,您应该使用路线(请参阅https://flutter.dev/docs/development/ui/navigation)此外,我推荐用于导航的插件Fluro(https://github.com/theyakka/氟)。这将使将参数传递到您的路线变得更容易。

为了更轻松地从推送消息中导航,请在 MaterialApp 中使用 navigatorKey

所以你的 MaterialApp Widget 看起来像这样(没有 Fluro):

class MyApp extends StatefulWidget {
  @override
  State createState() => _MyApp();
}

class _MyApp extends State<MyApp> {
  final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>(debugLabel:"navigator");
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: navigatorKey,
      home: MyHomePage(),
    );
  }

  @override
  void initState() {
    _firebaseMessaging.configure(
      //app in background
      onResume: (Map<String, dynamic> message) {
        if (message["type"] == "Notification") {
          final String id = message["Id"];
          final  String reference = message["reference"];
          navigatorKey.currentState.push(MaterialPageRoute(builder: (context) => DetailPage(Id: id, reference: reference)));
        }
      },
    );
  }
}
Run Code Online (Sandbox Code Playgroud)