flutter 从列表中抖动本地通知

zzzyeukh  于 2023-01-09  发布在  Flutter
关注(0)|答案(1)|浏览(162)

我正在使用flutter本地通知,一切都很好,但我试图显示随机字符串从一个列表中的通知,唯一的问题是通知显示相同的字符串一遍又一遍...我想显示每日通知,但字符串不一样。这里是我的代码:

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

这里我随机化字符串列表

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

这里的本地通知功能

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

在这里我显示通知每分钟,但问题是字符串不更新下一个通知,它只显示一个随机字符串一遍又一遍。
我怎样才能使这个函数在每次通知被调用时都能更新呢?提前谢谢!

11dmarpk

11dmarpk1#

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

相关问题