Dart 如何从推送通知重定向到颤振中的页面

Dart 如何从推送通知重定向到颤振中的页面,dart,flutter,Dart,Flutter,我正在我的颤振应用程序中实现推送通知,需要有关如何重定向的帮助 基本上,这是正常流量 app->root->home->list->detail。有一个默认的返回导航 当用户收到推送通知并单击它时,他们会直接转到详细信息页面。这是正确的,但我想看到一个返回按钮,他们可以进入主屏幕 应用程序->详细信息 我想要的是,在用户查看详细信息之后,他们应该返回主屏幕。然而,在这个场景中没有后退按钮 这是我的根页面 class RootPage extends StatefulWidget { Roo

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

基本上,这是正常流量

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 RootPage扩展StatefulWidget{
RootPage({Key}):超级(Key:Key);
_RootPage createState()=>\u RootPage();
}
类_RootPage使用WidgetsBindingObserver扩展状态{
AppLifecycleState(AppLifecycleState);
var_消息;
@凌驾
void didchangeAppifecyclestate(AppLifecycleState状态){
设置状态(){
_appLifecycleState=状态;
});
}
最终FirebaseMessaging_FirebaseMessaging=FirebaseMessaging();
@凌驾
void initState(){
super.initState();
WidgetsBinding.instance.addObserver(这个);
_firebaseMessaging.configure(
onMessage:(映射消息){
设置状态(){
_消息=消息;
});
},
//后台应用程序
onResume:(映射消息){
设置状态(){
_消息=消息;
});
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(声音:true、徽章:true、警报:true));
}
@凌驾
无效处置(){
WidgetsBinding.instance.removeObserver(此);
super.dispose();
}
@凌驾
小部件构建(构建上下文){
如果(_message!=null){
如果(_appLifecycleState!=null&&u appLifecycleState.index==0){
//应用程序处于恢复状态
如果(_消息[“类型”]=“通知”){
字符串_Id=_消息[“Id”];
字符串_reference=_message[“reference”];
返回(详细页(Id:_Id,参考:_参考));
}
}
}//如果结束,则结束
返回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;
    });
  }
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
@凌驾
void initState(){
super.initState();
}
最终列表_子项=[
列表(),
搜索(),
MyCalendarPage(),
];
var _currentIndex=0;
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:中心(子:文本(“我的应用”),
行动:[
新图标按钮(
图标:新图标(Icons.favorite),
按下:(){},
),
],
),
抽屉:抽屉(),
正文:_children[_currentIndex],
底部导航栏:底部导航栏(
类型:BottomNavigationBarType.fixed,
onTap:onTabTapped,//新建
currentIndex:_currentIndex,//新建
项目:[
新海底导航气压计(
图标:新图标(Icons.home),
标题:新文本(“主页”),
),
新海底导航气压计(
图标:新图标(Icons.search),
标题:新文本(“搜索”),
),
新海底导航气压计(
图标:新图标(图标.时间表),
标题:新文本(“附表”),
)
]
)
);
}
void onTabTapped(整数索引){
设置状态(){
_currentIndex=索引;
});
}
}
如果有推送通知,如何转到主屏幕,然后重定向到详细信息页面?我相信我会看到一个后退按钮,因为上一页是主屏幕

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


感谢您的帮助

如果您需要导航的后退按钮,您应该使用路线(请参阅) 此外,我推荐了用于导航的插件Fluro()。这将使你的路线更容易通行

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

因此,MaterialApp小部件的外观如下(没有fluro):

类MyApp扩展StatefulWidget{
@凌驾
State createState()=>\u MyApp();
}
类MyApp扩展了状态{
最终GlobalKey navigatorKey=GlobalKey(调试标签:“navigator”);
最终FirebaseMessaging_FirebaseMessaging=FirebaseMessaging();
@凌驾
小部件构建(构建上下文){
返回材料PP(
导航工作:导航工作,
主页:MyHomePage(),
);
}
@凌驾
void initState(){
_firebaseMessaging.configure(
//后台应用程序
onResume:(映射消息){
如果(消息[“类型”]=“通知”){
最终字符串id=消息[“id”];
最终字符串引用=消息[“引用”];
navigatorKey.currentState.push(materialpage(builder:(context)=>DetailPage(Id:Id,reference:reference));
}
},
);
}
}
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)));
        }
      },
    );
  }
}