using System.Collections.Generic; namespace Unity.Notifications.iOS { /// /// Use the iOSNotificationCenter to register notification channels and schedule local notifications. /// public class iOSNotificationCenter { private static bool s_Initialized; /// /// The delegate type for the notification received callbacks. /// public delegate void NotificationReceivedCallback(iOSNotification notification); /// /// Subscribe to this event to receive a callback whenever a local notification or a remote is shown to the user. /// public static event NotificationReceivedCallback OnNotificationReceived { add { if (!s_OnNotificationReceivedCallbackSet) { iOSNotificationsWrapper.RegisterOnReceivedCallback(); s_OnNotificationReceivedCallbackSet = true; } s_OnNotificationReceived += value; } remove { s_OnNotificationReceived -= value; } } private static bool s_OnNotificationReceivedCallbackSet; private static event NotificationReceivedCallback s_OnNotificationReceived = delegate { }; /// /// Subscribe to this event to receive a callback whenever a remote notification is received while the app is in foreground, /// if you subscribe to this event remote notification will not be shown while the app is in foreground and if you still want /// to show it to the user you will have to schedule a local notification with the data received from this callback. /// If you want remote notifications to be shown automatically subscribe to the [[OnNotificationReceived]] even instead and check the /// [[Notification.Trigger]] class type to determine whether the received notification is a remote notification. /// public static event NotificationReceivedCallback OnRemoteNotificationReceived { add { if (!s_OnRemoteNotificationReceivedCallbackSet) { iOSNotificationsWrapper.RegisterOnReceivedRemoteNotificationCallback(); s_OnRemoteNotificationReceivedCallbackSet = true; } s_OnRemoteNotificationReceived += value; } remove { s_OnRemoteNotificationReceived -= value; } } private static bool s_OnRemoteNotificationReceivedCallbackSet; private static event NotificationReceivedCallback s_OnRemoteNotificationReceived = delegate { }; internal delegate void AuthorizationRequestCompletedCallback(iOSAuthorizationRequestData data); /// /// The number currently set as the badge of the app icon. /// public static int ApplicationBadge { get { return iOSNotificationsWrapper.GetApplicationBadge(); } set { iOSNotificationsWrapper.SetApplicationBadge(value); } } static bool Initialize() { #if UNITY_EDITOR || !UNITY_IOS return false; #elif UNITY_IOS if (s_Initialized) return true; iOSNotificationsWrapper.RegisterOnReceivedCallback(); return s_Initialized = true; #endif } /// /// Schedules a local notification for delivery. /// /// Notification to schedule public static void ScheduleNotification(iOSNotification notification) { if (!Initialize()) return; iOSNotificationsWrapper.ScheduleLocalNotification(notification.GetDataForSending()); } /// /// Returns all notifications that are currently scheduled. /// /// Array of scheduled notifications public static iOSNotification[] GetScheduledNotifications() { return NotificationDataToNotifications(iOSNotificationsWrapper.GetScheduledNotificationData()); } /// /// Returns all of the app's delivered notifications that are currently shown in the Notification Center. /// /// Array of delivered notifications public static iOSNotification[] GetDeliveredNotifications() { return NotificationDataToNotifications(iOSNotificationsWrapper.GetDeliveredNotificationData()); } private static iOSNotification[] NotificationDataToNotifications(iOSNotificationWithUserInfo[] notificationData) { var iOSNotifications = new iOSNotification[notificationData == null ? 0 : notificationData.Length]; for (int i = 0; i < iOSNotifications.Length; ++i) iOSNotifications[i] = new iOSNotification(notificationData[i]); return iOSNotifications; } /// /// Use this to retrieve the last local or remote notification received by the app. /// /// /// /// Returns the last local or remote notification used to open the app or clicked on by the user. If no notification is available it returns null. /// public static iOSNotification GetLastRespondedNotification() { var data = iOSNotificationsWrapper.GetLastNotificationData(); if (data == null) return null; return new iOSNotification(data.Value); } /// /// Get users chosen action for the last actionable notification, null if no action was chosen. /// /// /// Action identifier public static string GetLastRespondedNotificationAction() { return iOSNotificationsWrapper.GetLastRespondedNotificationAction(); } /// /// Get users text input for the last actionable notification with input support, null if no input. /// /// Text user extered in the input field from notification public static string GetLastRespondedNotificationUserText() { return iOSNotificationsWrapper.GetLastRespondedNotificationUserText(); } /// /// Unschedules the specified notification. /// /// Identifier for the notification to be removed public static void RemoveScheduledNotification(string identifier) { if (Initialize()) iOSNotificationsWrapper._RemoveScheduledNotification(identifier); } /// /// Removes the specified notification from Notification Center. /// /// Identifier for the notification to be removed public static void RemoveDeliveredNotification(string identifier) { if (Initialize()) iOSNotificationsWrapper._RemoveDeliveredNotification(identifier); } /// /// Unschedules all pending notification. /// public static void RemoveAllScheduledNotifications() { if (Initialize()) iOSNotificationsWrapper._RemoveAllScheduledNotifications(); } /// /// Removes all of the app's delivered notifications from the Notification Center. /// public static void RemoveAllDeliveredNotifications() { if (Initialize()) iOSNotificationsWrapper._RemoveAllDeliveredNotifications(); } /// /// Get the notification settings for this app. /// /// Notification settings public static iOSNotificationSettings GetNotificationSettings() { return iOSNotificationsWrapper.GetNotificationSettings(); } /// /// Set (replace if already set) notification categories. /// Use this to setup actionable notifications. You can specify actions for each category, /// which then will be available for each notification with the same category identifier. /// Categories must be registered before sending notifications. /// /// All notification categories for your application public static void SetNotificationCategories(IEnumerable categories) { iOSNotificationsWrapper.SetNotificationCategories(categories); } internal static void OnReceivedRemoteNotification(iOSNotificationWithUserInfo data) { var notification = new iOSNotification(data); s_OnRemoteNotificationReceived(notification); } internal static void OnSentNotification(iOSNotificationWithUserInfo data) { var notification = new iOSNotification(data); s_OnNotificationReceived(notification); } /// /// Opens Settings. /// On iOS there is no way to open notification settings specifically, but you can open settings app with current application settings. /// Note, that application will be suspended, since opening settings is switching to different application. /// public static void OpenNotificationSettings() { #if !UNITY_EDITOR iOSNotificationsWrapper._OpenNotificationSettings(); #endif } } }