#if UNITY_ANDROID using System; using System.Collections; using Unity.Notifications.Android; namespace NotificationSamples.Android { /// /// Android implementation of . /// public class AndroidNotificationsPlatform : IGameNotificationsPlatform, IDisposable { /// public event Action NotificationReceived; /// /// Gets or sets the default channel ID for notifications. /// /// The default channel ID for new notifications, or null. public string DefaultChannelId { get; set; } /// /// Instantiate a new instance of . /// public AndroidNotificationsPlatform() { AndroidNotificationCenter.OnNotificationReceived += OnLocalNotificationReceived; } public IEnumerator RequestNotificationPermission() { var request = new PermissionRequest(); while (request.Status == PermissionStatus.RequestPending) yield return null; } /// /// /// Will set the field of . /// public void ScheduleNotification(AndroidGameNotification gameNotification) { if (gameNotification == null) { throw new ArgumentNullException(nameof(gameNotification)); } if (gameNotification.Id.HasValue) { AndroidNotificationCenter.SendNotificationWithExplicitID(gameNotification.InternalNotification, gameNotification.DeliveredChannel, gameNotification.Id.Value); } else { int notificationId = AndroidNotificationCenter.SendNotification(gameNotification.InternalNotification, gameNotification.DeliveredChannel); gameNotification.Id = notificationId; } gameNotification.OnScheduled(); } /// /// /// Will set the field of . /// public void ScheduleNotification(IGameNotification gameNotification) { if (gameNotification == null) { throw new ArgumentNullException(nameof(gameNotification)); } if (!(gameNotification is AndroidGameNotification androidNotification)) { throw new InvalidOperationException( "Notification provided to ScheduleNotification isn't an AndroidGameNotification."); } ScheduleNotification(androidNotification); } /// /// /// Create a new . /// public AndroidGameNotification CreateNotification() { var notification = new AndroidGameNotification() { DeliveredChannel = DefaultChannelId }; return notification; } /// /// /// Create a new . /// IGameNotification IGameNotificationsPlatform.CreateNotification() { return CreateNotification(); } /// public void CancelNotification(int notificationId) { AndroidNotificationCenter.CancelScheduledNotification(notificationId); } /// /// /// Not currently implemented on Android /// public void DismissNotification(int notificationId) { AndroidNotificationCenter.CancelDisplayedNotification(notificationId); } /// public void CancelAllScheduledNotifications() { AndroidNotificationCenter.CancelAllScheduledNotifications(); } /// public void DismissAllDisplayedNotifications() { AndroidNotificationCenter.CancelAllDisplayedNotifications(); } /// IGameNotification IGameNotificationsPlatform.GetLastNotification() { return GetLastNotification(); } /// public AndroidGameNotification GetLastNotification() { var data = AndroidNotificationCenter.GetLastNotificationIntent(); if (data != null) { return new AndroidGameNotification(data.Notification, data.Id, data.Channel); } return null; } /// /// Does nothing on Android. /// public void OnForeground() {} /// /// Does nothing on Android. /// public void OnBackground() {} /// /// Unregister delegates. /// public void Dispose() { AndroidNotificationCenter.OnNotificationReceived -= OnLocalNotificationReceived; } // Event handler for receiving local notifications. private void OnLocalNotificationReceived(AndroidNotificationIntentData data) { // Create a new AndroidGameNotification out of the delivered notification, but only // if the event is registered NotificationReceived?.Invoke(new AndroidGameNotification(data.Notification, data.Id, data.Channel)); } } } #endif