3

I am using an awesome Package and am trying to make a notification at a specific time using this package.

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,

            ),
        
     

i need to make notification in particular time.

3 Answers3

2

The same plugin provides a way to schedule notifications as per the need.

Checkout this link from it's description: https://pub.dev/packages/awesome_notifications#scheduling-a-notification

Bascially showing it at a specific time will look something like this:

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

Note: The code sample is from the plugin's Readme. I have not tested this yet.

Harshvardhan Joshi
  • 2,855
  • 2
  • 18
  • 31
0

I created an example for Daily Notification at 11:59:10 PM

(Exclude "repeats : true" if you don't want to repeat daily)

And Even You can specify these: [millisecond, weekday , weekofmonth , weekofyear ]

 schedule: NotificationCalendar(
                hour: 23,
                minute: 59,
                second: 10,
                repeats: true
              )

I checked its working fine :)

Complete Code :

 AwesomeNotifications().createNotification(
            content: NotificationContent(
                id: 13,
                channelKey: 'channelKey',
                title: 'title',
                body: 'this is body'), 
            schedule: NotificationCalendar(
                 hour: 23,
                 minute: 59,
                 second: 10,
                 repeats: true
                   )
                );
-2
Timer.periodic(Duration(minutes: 1), (timer) {
      if (DateTime.now()== DateTime.parse("2021-07-20 20:18:04Z")){// 8:18pm
        return showNotificationWithIconsAndActionButtons();
      }
    });

This will let it check the time every minute.