0

i m using flutter local notifications, everything is working fine, but i m trying to display random string from a list in the notifications, the only problem is the Notification showing the same string over and over again... i want to display daily notification but the string to not be the same. Here is my code:

String? randomName;
    final random = new Random();
    randomName = names[random.nextInt(names.length)];

Here i randomize the list of strings

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
    );
  }

Here the local notification function

 onPressed: () {
                      setState(() {
                        showToast();
                        NotificationService().showNotification(
                          1,
                          '$randomNames${widget.userPost}',
                          randomName!,
                        );
                      });
                    },

Here i display the notification every minute but the problem is the string is not updating on the next notification, it only show one random string over and over again.

How can i make the function to be updated every time when the notification it s called? Thx in advance!

Marian
  • 39
  • 3

1 Answers1

0
NotificationService().showNotification(
                      1,
                      '$randomNames${widget.userPost}',
                      names[Random(DateTime.now().microsecondsSinceEpoch).nextInt(names.length)],
                    );
Sophimp
  • 1
  • 2