dart - 如何从推送通知跳转到一个页面

标签 dart flutter

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

基本上,这是正常的流程

app->root->home->list->detail。有一个默认的后退导航

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

应用->详细信息

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

这是我的根页面

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

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

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

感谢帮助

最佳答案

如果你想要一个用于导航的后退按钮,你应该使用路由(参见 https://flutter.dev/docs/development/ui/navigation ) 此外,我还推荐用于导航的插件 Fluro (https://github.com/theyakka/fluro)。这将使传递参数到您的路线更容易。

要使从推送消息导航更容易,请在您的 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)));
        }
      },
    );
  }
}

关于dart - 如何从推送通知跳转到一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56237373/

相关文章:

android - flutter initstate() 列表未访问另一个类小部件

git - VS Code 中文件名旁边的 "A "是什么意思?

firebase - FCM token - 我什么时候应该在我的数据库中存储/保存它?

dart - Assets 文件夹中的 Flutter Excel 工作表

date - 将纪元时间转换为时间戳 flutter

firebase - 如何使用DocumentSnapshot而不是QuerySnapshot?

firebase - 不能将 'AuthResult'类型的值分配给 'FirebaseUser'类型的变量

flutter - 配置单元registerAdapter不接受ID

dart - flutter 导航栏

flutter - 加载数据时制作骨架屏幕的最简单方法