Skip to content

Commit

Permalink
#94 repeat notification to be delivered after the specified amount of…
Browse files Browse the repository at this point in the history
… time elapses
  • Loading branch information
thudugala committed Jun 29, 2020
1 parent 071eba5 commit cae5f67
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
7 changes: 6 additions & 1 deletion Source/Plugin.LocalNotification/NotificationRepeat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public enum NotificationRepeat
/// <summary>
/// Notification should repeat next week at same day, same time
/// </summary>
Weekly
Weekly,

/// <summary>
/// Notification to be delivered after the specified amount of time elapses
/// </summary>
TimeInterval
}
}
7 changes: 6 additions & 1 deletion Source/Plugin.LocalNotification/NotificationRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ public class NotificationRequest
public DateTime? NotifyTime { get; set; }

/// <summary>
/// If true, will repeat again at the time specifies in NotifyTime.
/// if Repeats = TimeInterval, then repeat again after specified amount of time elapses
/// </summary>
public TimeSpan? NotifyRepeatInterval { get; set; }

/// <summary>
/// If true, will repeat again at the time specifies in NotifyTime or NotifyRepeatInterval
/// </summary>
public NotificationRepeat Repeats { get; set; } = NotificationRepeat.No;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public override Result DoWork()
{
notification.NotifyTime = notification.NotifyTime.Value.AddDays(1);
}

break;

case NotificationRepeat.Weekly:
Expand All @@ -66,7 +65,18 @@ public override Result DoWork()
{
notification.NotifyTime = notification.NotifyTime.Value.AddDays(7);
}
break;

case NotificationRepeat.TimeInterval:
if (notification.NotifyRepeatInterval.HasValue)
{
notification.NotifyTime =
notification.NotifyTime.Value.Add(notification.NotifyRepeatInterval.Value);
while (notification.NotifyTime <= DateTime.Now)
{
notification.NotifyTime = notification.NotifyTime.Value.Add(notification.NotifyRepeatInterval.Value);
}
}
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public void CancelAll()
/// <inheritdoc />
public async void Show(NotificationRequest notificationRequest)
{
UNNotificationTrigger trigger = null;
try
{
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0) == false)
Expand Down Expand Up @@ -108,8 +109,20 @@ public async void Show(NotificationRequest notificationRequest)
}

var repeats = notificationRequest.Repeats != NotificationRepeat.No;
using var notifyTime = GetNsDateComponentsFromDateTime(notificationRequest);
using var trigger = UNCalendarNotificationTrigger.CreateTrigger(notifyTime, repeats);

if (repeats && notificationRequest.Repeats == NotificationRepeat.TimeInterval &&
notificationRequest.NotifyRepeatInterval.HasValue)
{
var seconds = notificationRequest.NotifyRepeatInterval.Value.TotalSeconds;

trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(seconds, true);
}
else
{
using var notifyTime = GetNsDateComponentsFromDateTime(notificationRequest);
trigger = UNCalendarNotificationTrigger.CreateTrigger(notifyTime, repeats);
}

var notificationId =
notificationRequest.NotificationId.ToString(CultureInfo.CurrentCulture);

Expand All @@ -122,6 +135,10 @@ await UNUserNotificationCenter.Current.AddNotificationRequestAsync(request)
{
Debug.WriteLine(ex);
}
finally
{
trigger?.Dispose();
}
}

private static NSDateComponents GetNsDateComponentsFromDateTime(NotificationRequest notificationRequest)
Expand Down

0 comments on commit cae5f67

Please sign in to comment.