当应用程序被杀死时,Flutter 本地通知不会重定向到相关屏幕
Flutter local notifications doesn't redirect to relevant screen when app is killed
我在 Flutter 应用程序中使用 flutter_local_notifications
在特定时间安排通知。
当我点击通知时,应用程序 运行 它按预期工作并进入相关屏幕;但是当应用程序被杀死时点击通知时,它会转至主屏幕。
由于此通知是自动安排的,而不是在 user-action 上安排的,所以我是在一个函数中而不是在一个小部件中进行的。所以,我没有context
。为了解决这个问题,我使用 NavigatorState
类型的 GlobalKey
。
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
navigatorKey.currentState?.push(MaterialPageRoute(builder: (ctx) {
return Scaffold(
appBar: AppBar(
title: Text('notification clicked'),
),
);
}));
我尝试的第二种方法是使用有状态小部件
class Noti extends StatefulWidget {
const Noti({Key? key}) : super(key: key);
@override
NotiState createState() => NotiState();
}
class NotiState extends State<Noti> {
@override
void initState() {
super.initState();
initNotification();
}
Future initNotification() async {
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('bolt');
final InitializationSettings initializationSettings =
InitializationSettings(android: initializationSettingsAndroid);
flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onSelectNotification: selectNotification,
);
const notDetails = NotificationDetails(
android: AndroidNotificationDetails('1', 'Scheduled Review',
channelDescription: 'Review reminders at scheduled time'));
flutterLocalNotificationsPlugin.periodicallyShow(1, 'Scheduled Review',
'Every Minute', RepeatInterval.everyMinute, notDetails);
}
Future selectNotification(String? payload) async {
Navigator.push(context, MaterialPageRoute(builder: ((ctx) {
return Scaffold(
appBar: AppBar(
title: Text('notification clicked'),
),
);
})));
}
@override
Widget build(BuildContext context) {
return Container();
}
}
我将这个 Noti
小部件插入到我主页的随机位置,以便它的 initState
被调用。
同样的事情也发生在这里;即,当应用程序死机时单击通知,我将被重定向到主屏幕,而不是标题为 notification clicked
的屏幕,就像这里的逻辑一样。当应用程序处于活动状态时,它可以正常工作。
我在上述库的两个版本 8.2.0
和 9.0.0
上都试过了。
没看到这条留言:
Note: from version 4.0 of the plugin, calling initialize will not trigger the onSelectNotification callback when the application was started by tapping on a notification to trigger. Use the getNotificationAppLaunchDetails method that is available in the plugin if you need to handle a notification triggering the launch for an app e.g. change the home route of the app for deep-linking.
在 notificationAppLaunchDetails
的基础上更改 initialRoute
解决了它。
final NotificationAppLaunchDetails? notificationAppLaunchDetails =
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
final didNotificationLaunchApp =
notificationAppLaunchDetails?.didNotificationLaunchApp ?? false;
final initialRoute = didNotificationLaunchApp ? '/second' : '/first';
我在 Flutter 应用程序中使用 flutter_local_notifications
在特定时间安排通知。
当我点击通知时,应用程序 运行 它按预期工作并进入相关屏幕;但是当应用程序被杀死时点击通知时,它会转至主屏幕。
由于此通知是自动安排的,而不是在 user-action 上安排的,所以我是在一个函数中而不是在一个小部件中进行的。所以,我没有context
。为了解决这个问题,我使用 NavigatorState
类型的 GlobalKey
。
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
navigatorKey.currentState?.push(MaterialPageRoute(builder: (ctx) {
return Scaffold(
appBar: AppBar(
title: Text('notification clicked'),
),
);
}));
我尝试的第二种方法是使用有状态小部件
class Noti extends StatefulWidget {
const Noti({Key? key}) : super(key: key);
@override
NotiState createState() => NotiState();
}
class NotiState extends State<Noti> {
@override
void initState() {
super.initState();
initNotification();
}
Future initNotification() async {
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('bolt');
final InitializationSettings initializationSettings =
InitializationSettings(android: initializationSettingsAndroid);
flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onSelectNotification: selectNotification,
);
const notDetails = NotificationDetails(
android: AndroidNotificationDetails('1', 'Scheduled Review',
channelDescription: 'Review reminders at scheduled time'));
flutterLocalNotificationsPlugin.periodicallyShow(1, 'Scheduled Review',
'Every Minute', RepeatInterval.everyMinute, notDetails);
}
Future selectNotification(String? payload) async {
Navigator.push(context, MaterialPageRoute(builder: ((ctx) {
return Scaffold(
appBar: AppBar(
title: Text('notification clicked'),
),
);
})));
}
@override
Widget build(BuildContext context) {
return Container();
}
}
我将这个 Noti
小部件插入到我主页的随机位置,以便它的 initState
被调用。
同样的事情也发生在这里;即,当应用程序死机时单击通知,我将被重定向到主屏幕,而不是标题为 notification clicked
的屏幕,就像这里的逻辑一样。当应用程序处于活动状态时,它可以正常工作。
我在上述库的两个版本 8.2.0
和 9.0.0
上都试过了。
没看到这条留言:
Note: from version 4.0 of the plugin, calling initialize will not trigger the onSelectNotification callback when the application was started by tapping on a notification to trigger. Use the getNotificationAppLaunchDetails method that is available in the plugin if you need to handle a notification triggering the launch for an app e.g. change the home route of the app for deep-linking.
在 notificationAppLaunchDetails
的基础上更改 initialRoute
解决了它。
final NotificationAppLaunchDetails? notificationAppLaunchDetails =
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
final didNotificationLaunchApp =
notificationAppLaunchDetails?.didNotificationLaunchApp ?? false;
final initialRoute = didNotificationLaunchApp ? '/second' : '/first';