如何使for循环在UI之外颤动?

时间:2018-10-09 16:51:31

标签: flutter

我有一个for循环,可在一天的每一小时的每一分钟设置通知,

代码:

Future _showCollectionNotifications(int times, bool isHourly) async {
    var groupKey = 'com.ofo.app.collection';
    var android = AndroidNotificationDetails(
      'Id',
      'Name',
      'Description',
      groupKey: groupKey,
      setAsGroupSummary: true,
      channelShowBadge: true,
    );
    var iOS = IOSNotificationDetails();
    var details = NotificationDetails(android, iOS);
    String title = 'Collection';
    if (isHourly) {
      for (int id = 0; id < 24; id += times) {
        Time time = Time(id, 0, 0);
        String body = collectionLists[7].Collections[_getRandomCollection()];
        await randomNotification.showDailyAtTime(
            id, title, body, time, details);
      }
    } else {
      for (int id = 0; id < 24; id++) {
        for (int i = 0; i < 60; i += times) {
          Time time = Time(id, i, 0);
          String body = collectionLists[7].collections[_getRandomCollection()];
          await randomNotification.showDailyAtTime(
              id, title, body, time, details);
        }
      }
    }
  }

问题是它冻结了应用程序,因此,如果可以某种方式在后台或UI外部运行代码,则可以解决问题。

谢谢。

2 个答案:

答案 0 :(得分:0)

在这种情况下,您应该尝试使用flutter_local_notifications,它将为您提供:

  • 计划在指定日期每周显示一次通知, 时间
  • 安排每天在指定时间显示的通知

文档示例:每分钟显示通知

// Show a notification every minute with the first appearance happening a minute after invoking the method
var androidPlatformChannelSpecifics =
    new AndroidNotificationDetails('repeating channel id',
        'repeating channel name', 'repeating description');
var iOSPlatformChannelSpecifics =
    new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
    androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.periodicallyShow(0, 'repeating title',
    'repeating body', RepeatInterval.EveryMinute, platformChannelSpecifics);

答案 1 :(得分:0)

由于您要定期发送通知,因此可以使用 NUM CT ---------- ---------- 128 5 129 5 130 5 131 5 132 5 137 6 138 6 139 6 140 6 141 6 142 6 生成通知。

我附上一个示例,您可以将其用于Stream.periodic方法(我在流中使用秒来更轻松地对其进行测试)。

您可以自定义showCollectionNotifications()类以包含所需的任何内容。

Notification