using System; #if UNITY_ANDROID using PlatformNotification = Unity.Notifications.Android.AndroidNotification; #else using Unity.Notifications.iOS; using PlatformNotification = Unity.Notifications.iOS.iOSNotification; #endif namespace Unity.Notifications { /// /// Interval, at which notification should repeat. /// public enum NotificationRepeatInterval { /// /// Indicates, that notification does not repeat. /// OneTime = 0, /// /// Indicates, that notification should repeat daily. /// When used in , only time is used for scheduling. /// Daily = 1, } /// /// Marker interface for different schedule types. /// public interface NotificationSchedule { internal void Schedule(ref PlatformNotification notification); } /// /// Schedule notification to show up after a certain amount of time, optionally repeating at the same time interval. /// public struct NotificationIntervalSchedule : NotificationSchedule { /// /// Time interval to show notification from current time. /// Only full seconds are considered. /// public TimeSpan Interval { get; set; } /// /// Whether notification should repeat. /// If true, notification will repeat at the same interval as initial time from the current one. /// public bool Repeats { get; set; } /// /// Convenience constructor. /// /// Value for /// Value for public NotificationIntervalSchedule(TimeSpan interval, bool repeats = false) { Interval = interval; Repeats = repeats; } void NotificationSchedule.Schedule(ref PlatformNotification notification) { #if UNITY_ANDROID notification.FireTime = DateTime.Now + Interval; if (Repeats) notification.RepeatInterval = Interval; #else notification.Trigger = new iOSNotificationTimeIntervalTrigger() { TimeInterval = Interval, Repeats = Repeats, }; #endif } } /// /// Schedule to show notification at particular date and time. /// Optionally can repeat at predefined intervals. /// public struct NotificationDateTimeSchedule : NotificationSchedule { /// /// Date and time when notification has to be shown if does not repeat. /// If notification is set to repeat, the meaning of this value depends on . /// public DateTime FireTime { get; set; } /// /// Interval, at which notification should repeat from the first delivery. /// public NotificationRepeatInterval RepeatInterval { get; set; } /// /// Convenience constructor. /// /// Value for /// Value for public NotificationDateTimeSchedule(DateTime fireTime, NotificationRepeatInterval repeatInterval = NotificationRepeatInterval.OneTime) { FireTime = fireTime; RepeatInterval = repeatInterval; } void NotificationSchedule.Schedule(ref PlatformNotification notification) { #if UNITY_ANDROID // TODO handle UTC switch (RepeatInterval) { case NotificationRepeatInterval.OneTime: notification.FireTime = FireTime; break; case NotificationRepeatInterval.Daily: { var currentTime = DateTime.Now; var fireTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, FireTime.Hour, FireTime.Minute, FireTime.Second); if (fireTime < currentTime) fireTime = fireTime.AddDays(1); notification.FireTime = fireTime; notification.RepeatInterval = TimeSpan.FromDays(1); break; } } #else var trigger = new iOSNotificationCalendarTrigger() { Hour = FireTime.Hour, Minute = FireTime.Minute, Second = FireTime.Second, UtcTime = FireTime.Kind == DateTimeKind.Utc, }; switch (RepeatInterval) { case NotificationRepeatInterval.OneTime: trigger.Year = FireTime.Year; trigger.Month = FireTime.Month; trigger.Day = FireTime.Day; break; case NotificationRepeatInterval.Daily: trigger.Day = null; trigger.Repeats = true; break; default: throw new Exception($"Unsupported repeat interval {RepeatInterval}"); } notification.Trigger = trigger; #endif } } }