【问题标题】:How to redirect to a page in flutter from push notification如何从推送通知重定向到颤动的页面
【发布时间】:2019-10-07 19:03:30
【问题描述】:

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

基本上,这是正常的流程

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

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

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

感谢您的帮助

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      • 1970-01-01
      • 2019-04-29
      • 1970-01-01
      • 2021-01-25
      • 2022-01-20
      相关资源
      最近更新 更多