i m trying to make an app with notifications which display daily notifications to the user. I m using local notification plugin, everything is working but it repeat the same notification again and again, for me i want to show different notification each day.
Future<void> showNotification(int id, String title, String body) async {
await flutterLocalNotificationsPlugin.periodicallyShow(
id,
title,
body,
RepeatInterval
.everyMinute, //schedule the notification to show after 2 seconds.
const NotificationDetails(
// Android details
android: AndroidNotificationDetails('main_channel', 'Main Channel',
channelDescription: "notfi",
importance: Importance.max,
priority: Priority.max),
// iOS details
iOS: DarwinNotificationDetails(
sound: 'default.wav',
presentAlert: true,
presentBadge: true,
presentSound: true,
),
),
// Type of time interpretation
androidAllowWhileIdle:
true, // To show notification even when the app is closed
);
}
Here is my code for schedule notification daily
String? randomName;
final random = new Random();
randomName = names[random.nextInt(names.length)];
Here i randomize a list from where i want to get random string from it. I don' t know why it s not updating every day.
onPressed: () {
setState(() {
showToast();
NotificationService().showNotification(
1,
'$randomNames${widget.userPost}',
开发者_开发百科 randomName!,
);
});
},
Here is where i call the notifications, it s working, but it get me the same notification over and over again.
Anybody know how to solve this?
I've tried with setState
To get what you want you have to use zoneSchedule, and loop for the notifications you want to get and also set the date of receipt.
Here is my code :
scheduleNotification(
DateTime date, String title, String description, int id) async {
var tzDateNotif = tz.TZDateTime.from(date, tz.local);
await _localNotificationService.zonedSchedule(
id,
title,
description,
tzDateNotif,
const NotificationDetails(
android: AndroidNotificationDetails(
'your channel id', 'your channel description')),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time,
);
}
And for the loop you can do like this :
var notifications = [
{
"title": "title1",
"description": "description 1",
"date" : "the date that you want to receive the 1st notif",
"id": "id1"
},
{
"title": "title2",
"description": "description 2",
"date" : "the date that you want to receive the 2nd notif",
"id": "id2"
},
{
"title": "title3",
"description": "description 3",
"date" : "the date that you want to receive the 3rd notif",
"id": "id3"
},
{
"title": "title4",
"description": "description 4",
"date" : "the date that you want to receive the 4th notif",
"id": "id4"
},
];
for (notif in notifications) {
localNotificationService.scheduleNotification(
notif["date"], notif["title"], notif["description"], notif["id"]);
}
For the id it must be different for each notification.
精彩评论