问题
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