I open the app through the notification and want to keep it open after the user presses back?

谁说我不能喝 提交于 2019-12-11 18:00:19

问题


I have an app that has a notification system developed with OneSignal.

Use case: - When the notification comes with the app closed and the user clicks on it the app directly opens the screen corresponding to that notification, (Until then I got it!) But when the user clicks back, the app is closed, because there is only that route in the pile;

I want to somehow reopen the app or keep it open!

Something similar to what happens in Whatsapp, because when the app is closed and opened by direct notification in a chat, when it comes back it closes and makes the app open animation again!

Could someone help me with this? Or at least enlighten me. Thanks!


回答1:


That seems like a use case for the WillPopScope widget. this widget will tell you if the user has pressed the back button. you just need to wrap your scaffold with it.

here is an example of how to use it:

class MessagingPage extends StatefulWidget {
  @override
  _MessagingPageState createState() => _MessagingPageState();
}

class _MessagingPageState extends State<MessagingPage> {

  bool hasComeFromNotification = true;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: (){
        if(hasComeFromNotification){
          Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context){
            return HomePage();
          }));
          return Future.value(false); // do not call Navigator.pop() because I already called it
        } else {
          return Future.value(true); // call Navigator.pop()
        }
      },
      child: Scaffold(
        body: Center(
          child: Text('messaging'),
        ),
      ),
    );
  }
}


来源:https://stackoverflow.com/questions/58000349/i-open-the-app-through-the-notification-and-want-to-keep-it-open-after-the-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!