I m stuck at this point. I m trying for 2 days to figure it out how can i reach that. I m trying to get random notifications for the user, everything is working fine, but when i call the notifications it display the same String over and over again and it's not updating anymore. How can the code should look like to be updated and showing other String from the List without touching the button again?
String? randomName;
final random = new Random();
randomName = names[random.nextInt(names.length)];
onPressed: () {
showToast();
NotificationService()
.showNotification(1, 'Hello', randomName!);
},
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: "ashwin",
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
);
}
I've been trying using setState but it's not working...
CodePudding user response:
A periodic notification uses a repeated title and a repeated body. It's meant to show the same notification with the same content over and over again. If you want to dynamically show content, you need a regular notification that fetches data in a random way from your list.
Take a look at this question, different scenario but same concept: How to change notification data like title and body, when using flutter_local_notification plugin?
CodePudding user response:
You can try generating the random name in the same scope as showing the notification like so:
onPressed: () {
String? randomName;
final random = new Random();
randomName = names[random.nextInt(names.length)];
showToast();
NotificationService()
.showNotification(1, 'Hello', randomName!);
}