如何从推送通知中快速重定向到页面

时间:2019-05-21 11:33:06

标签: dart flutter

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

基本上,这是正常流量

app-> root-> home-> list-> details 。有默认的后退导航

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

应用程序->详细信息

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

这是我的根页面

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();
  }
}

主页

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;
    });
  }
}

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

不确定如何重新编写代码以实现此目的

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

如果要使用后退按钮进行导航,则应使用路线(请参见https://flutter.dev/docs/development/ui/navigation) 另外,我推荐插件Fluro for Navigation(https://github.com/theyakka/fluro)。这样可以更轻松地将aruing传递到您的路线。

要使从推送消息中导航更容易,请在MaterialApp中使用navigatorKey

因此,您的MaterialApp窗口小部件将如下所示(没有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)));
        }
      },
    );
  }
}