【发布时间】:2020-02-26 08:22:56
【问题描述】:
有一个预定的通知应该在 5 秒后显示。这个预定的通知在void initState 函数内部被调用。我正在使用这个package 来显示通知。
通知恰好在 5 秒之后显示,因此没有问题。
问题是点击通知时应该调用另一个函数。但是这个函数在通知出现之前很久就被调用了,我不知道这是怎么回事。我尝试了不同的方法来解决这个问题,但都不起作用。
低调是发生这一切的代码。
class _HomePageState extends State<HomePage> {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
@override
void initState() {
super.initState();
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
var android = new AndroidInitializationSettings('@mipmap/ic_launcher');
var initNotifSettings = new InitializationSettings(android, null);
flutterLocalNotificationsPlugin.initialize(initNotifSettings,
onSelectNotification: whenNotificationSelect);
showNotification();
}
Future whenNotificationSelect(String payload) async {
print('Payload: $payload');
Navigator.pushNamed(context, '/notifications');
}
showNotification() async {
var android = new AndroidNotificationDetails(
'channel id', 'channel name', 'channel description');
var platform = new NotificationDetails(android, null);
var scheduledNotificationDateTime =
DateTime.now().add(Duration(seconds: 2));
await flutterLocalNotificationsPlugin.schedule(
0,
'Good morning!',
'Greetings from me.',
scheduledNotificationDateTime,
platform,
payload: 'Simple App',
);
}
}
注意函数whenNotificationSelect需要在点击通知时调用,但由于其他原因我不知道该函数在应用启动时立即被调用。 我希望 whenNotificationSelect 仅在点击通知时调用,而不是在应用启动时调用。
谢谢你,太爱了。
【问题讨论】:
标签: android flutter dart push-notification scheduling