#if UNITY_IOS using System; using Unity.Notifications.iOS; using UnityEngine; using UnityEngine.Assertions; namespace NotificationSamples.iOS { /// /// iOS implementation of . /// public class iOSGameNotification : IGameNotification { private readonly iOSNotification internalNotification; /// /// Gets the internal notification object used by the mobile notifications system. /// public iOSNotification InternalNotification => internalNotification; /// /// /// Internally stored as a string. Gets parsed to an integer when retrieving. /// /// The identifier as an integer, or null if the identifier couldn't be parsed as a number. public int? Id { get { if (!int.TryParse(internalNotification.Identifier, out int value)) { Debug.LogWarning("Internal iOS notification's identifier isn't a number."); return null; } return value; } set { if (value == null) { throw new ArgumentNullException(nameof(value)); } internalNotification.Identifier = value.Value.ToString(); } } /// public string Title { get => internalNotification.Title; set => internalNotification.Title = value; } /// public string Body { get => internalNotification.Body; set => internalNotification.Body = value; } /// public string Subtitle { get => internalNotification.Subtitle; set => internalNotification.Subtitle = value; } /// public string Data { get => internalNotification.Data; set => internalNotification.Data = value; } /// /// /// On iOS, this represents the notification's Category Identifier. /// /// The value of . public string Group { get => CategoryIdentifier; set => CategoryIdentifier = value; } /// public int? BadgeNumber { get => internalNotification.Badge != -1 ? internalNotification.Badge : (int?)null; set => internalNotification.Badge = value ?? -1; } /// public bool ShouldAutoCancel { get; set; } /// public bool Scheduled { get; private set; } /// /// /// On iOS, setting this causes the notification to be delivered on a calendar time. /// If it has previously been manually set to a different type of trigger, or has not been set before, /// this returns null. /// The millisecond component of the provided DateTime is ignored. /// /// A representing the delivery time of this message, or null if /// not set or the trigger isn't a . public DateTime? DeliveryTime { get { if (!(internalNotification.Trigger is iOSNotificationCalendarTrigger calendarTrigger)) { return null; } DateTime now = DateTime.Now; var result = new DateTime ( calendarTrigger.Year ?? now.Year, calendarTrigger.Month ?? now.Month, calendarTrigger.Day ?? now.Day, calendarTrigger.Hour ?? now.Hour, calendarTrigger.Minute ?? now.Minute, calendarTrigger.Second ?? now.Second, calendarTrigger.UtcTime ? DateTimeKind.Utc : DateTimeKind.Local ); return result.ToLocalTime(); } set { if (!value.HasValue) { return; } DateTime date = value.Value.ToLocalTime(); internalNotification.Trigger = new iOSNotificationCalendarTrigger { Year = date.Year, Month = date.Month, Day = date.Day, Hour = date.Hour, Minute = date.Minute, Second = date.Second }; } } /// /// The category identifier for this notification. /// public string CategoryIdentifier { get => internalNotification.CategoryIdentifier; set => internalNotification.CategoryIdentifier = value; } /// /// Does nothing on iOS. /// public string SmallIcon { get => null; set {} } /// /// Does nothing on iOS. /// public string LargeIcon { get => null; set {} } /// /// Instantiate a new instance of . /// public iOSGameNotification() { internalNotification = new iOSNotification { ShowInForeground = true // Deliver in foreground by default }; } /// /// Instantiate a new instance of from a delivered notification. /// /// The delivered notification. internal iOSGameNotification(iOSNotification internalNotification) { this.internalNotification = internalNotification; } /// /// Mark this notifications scheduled flag. /// internal void OnScheduled() { Assert.IsFalse(Scheduled); Scheduled = true; } } } #endif