3

我正在使用一个很棒的包,并试图在特定时间使用这个包发出通知。

Future<void> showNotificationWithIconsAndActionButtons(int id) async {
    AwesomeNotifications().initialize(
        '',
        [
          NotificationChannel(
              channelKey: 'basic_channel',
              channelName: 'Basic notifications',
              channelDescription: 'Notification channel for basic tests',
              defaultColor: Color(0xFF9D50DD),
              ledColor: Colors.white,
            playSound: true,

            importance: NotificationImportance.Max,
            defaultRingtoneType: DefaultRingtoneType.Notification,

          )
        ]
    );
 
    await AwesomeNotifications().createNotification(

        content: NotificationContent(
            id: id,
            channelKey: 'basic_channel',
            title: 'Anonymous says:',
            body: 'Hi there!',
            payload: {'uuid': 'user-profile-uuid'},
          displayOnBackground: true,
          displayOnForeground: true,

            ),
        
     

我需要在特定时间发出通知。

4

2 回答 2

1

同一个插件提供了一种根据需要安排通知的方法。

从它的描述中签出这个链接: https ://pub.dev/packages/awesome_notifications#scheduling-a-notification

基本上在特定时间显示它看起来像这样:

 await AwesomeNotifications().createNotification(
  content: NotificationContent(
    id: id,
    channelKey: 'scheduled',
    title: 'Just in time!',
    body: 'This notification was schedule to shows at ' +
        (Utils.DateUtils.parseDateToString(scheduleTime.toLocal()) ?? '?') +
        ' $timeZoneIdentifier (' +
        (Utils.DateUtils.parseDateToString(scheduleTime.toUtc()) ?? '?') +
        ' utc)',
    notificationLayout: NotificationLayout.BigPicture,
    bigPicture: 'asset://assets/images/delivery.jpeg',
    payload: {'uuid': 'uuid-test'},
    autoCancel: false,
  ),
  schedule: NotificationCalendar.fromDate(date: scheduleTime));

注意:代码示例来自插件的自述文件。我还没有测试过这个。

于 2021-07-18T16:58:05.620 回答
0
Timer.periodic(Duration(minutes: 1), (timer) {
      if (DateTime.now()== DateTime.parse("2021-07-20 20:18:04Z")){// 8:18pm
        return showNotificationWithIconsAndActionButtons();
      }
    });

这将让它每分钟检查一次时间。

于 2021-07-18T17:29:13.163 回答