#if UNITY_IOS
using System;
using System.Collections;
using Unity.Notifications.iOS;
namespace NotificationSamples.iOS
{
///
/// iOS implementation of .
///
public class iOSNotificationsPlatform : IGameNotificationsPlatform,
IDisposable
{
///
public event Action NotificationReceived;
///
/// Instantiate a new instance of .
///
public iOSNotificationsPlatform()
{
iOSNotificationCenter.OnNotificationReceived += OnLocalNotificationReceived;
}
public IEnumerator RequestNotificationPermission()
{
using (var request = new AuthorizationRequest(AuthorizationOption.Badge | AuthorizationOption.Sound | AuthorizationOption.Alert, false))
{
while (!request.IsFinished)
yield return null;
}
}
///
public void ScheduleNotification(IGameNotification gameNotification)
{
if (gameNotification == null)
{
throw new ArgumentNullException(nameof(gameNotification));
}
if (!(gameNotification is iOSGameNotification notification))
{
throw new InvalidOperationException(
"Notification provided to ScheduleNotification isn't an iOSGameNotification.");
}
ScheduleNotification(notification);
}
///
public void ScheduleNotification(iOSGameNotification notification)
{
if (notification == null)
{
throw new ArgumentNullException(nameof(notification));
}
iOSNotificationCenter.ScheduleNotification(notification.InternalNotification);
notification.OnScheduled();
}
///
///
/// Create a new .
///
IGameNotification IGameNotificationsPlatform.CreateNotification()
{
return CreateNotification();
}
///
///
/// Create a new .
///
public iOSGameNotification CreateNotification()
{
return new iOSGameNotification();
}
///
public void CancelNotification(int notificationId)
{
iOSNotificationCenter.RemoveScheduledNotification(notificationId.ToString());
}
///
public void DismissNotification(int notificationId)
{
iOSNotificationCenter.RemoveDeliveredNotification(notificationId.ToString());
}
///
public void CancelAllScheduledNotifications()
{
iOSNotificationCenter.RemoveAllScheduledNotifications();
}
///
public void DismissAllDisplayedNotifications()
{
iOSNotificationCenter.RemoveAllDeliveredNotifications();
}
///
IGameNotification IGameNotificationsPlatform.GetLastNotification()
{
return GetLastNotification();
}
///
public iOSGameNotification GetLastNotification()
{
var notification = iOSNotificationCenter.GetLastRespondedNotification();
if (notification != null)
{
return new iOSGameNotification(notification);
}
return null;
}
///
/// Clears badge count.
///
public void OnForeground()
{
iOSNotificationCenter.ApplicationBadge = 0;
}
///
/// Does nothing on iOS.
///
public void OnBackground() {}
///
/// Unregister delegates.
///
public void Dispose()
{
iOSNotificationCenter.OnNotificationReceived -= OnLocalNotificationReceived;
}
// Event handler for receiving local notifications.
private void OnLocalNotificationReceived(iOSNotification notification)
{
// Create a new AndroidGameNotification out of the delivered notification, but only
// if the event is registered
NotificationReceived?.Invoke(new iOSGameNotification(notification));
}
}
}
#endif