由于我可以创建的待处理本地通知数量受到限制,因此我只希望在特定时间设置一个本地通知,并在显示给用户时设置下一个本地通知,依此类推。 。我找不到任何方法可以在Flutter本地通知中监听onShown事件。有什么解决方法吗?
答案 0 :(得分:0)
您可以配置为在用户实际看到当前通知时显示下一个通知。请参见下面的代码。该代码取自flutter_local_notifications
软件包。
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
...
_configureDidReceiveLocalNotificationSubject(); // user is shown alert dialogue
...
}
@override
void dispose() {
didReceiveLocalNotificationSubject.close();
...
super.dispose();
}
// This is where the alertDialogue is shown to the user
void _configureDidReceiveLocalNotificationSubject() {
didReceiveLocalNotificationSubject.stream
.listen((ReceivedNotification receivedNotification) async {
await showDialog(
...
);
// Now the notification is shown, set the Next Notification
__scheduleNotification();
// This will be in a cycle, add an if condition to avoid infinite loop of notification.
// You can pass duration as parameter if needed.
});
}
/// Schedules a notification that specifies a different icon, sound and vibration pattern
Future<void> _scheduleNotification() async {
var scheduledNotificationDateTime =
DateTime.now().add(Duration(seconds: 5));
var vibrationPattern = Int64List(4);
vibrationPattern[0] = 0;
vibrationPattern[1] = 1000;
vibrationPattern[2] = 5000;
vibrationPattern[3] = 2000;
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your other channel id',
'your other channel name',
'your other channel description',
icon: 'secondary_icon',
sound: 'slow_spring_board',
largeIcon: 'sample_large_icon',
largeIconBitmapSource: BitmapSource.Drawable,
vibrationPattern: vibrationPattern,
enableLights: true,
color: const Color.fromARGB(255, 255, 0, 0),
ledColor: const Color.fromARGB(255, 255, 0, 0),
ledOnMs: 1000,
ledOffMs: 500);
var iOSPlatformChannelSpecifics =
IOSNotificationDetails(sound: 'slow_spring_board.aiff');
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.schedule(
0,
'scheduled title',
'scheduled body',
scheduledNotificationDateTime,
platformChannelSpecifics);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
...
);
}