fix:1、更换sdk
This commit is contained in:
+222
@@ -0,0 +1,222 @@
|
||||
using System;
|
||||
|
||||
#if UNITY_ANDROID
|
||||
using PlatformNotification = Unity.Notifications.Android.AndroidNotification;
|
||||
#else
|
||||
using System.Globalization;
|
||||
using PlatformNotification = Unity.Notifications.iOS.iOSNotification;
|
||||
#endif
|
||||
|
||||
namespace Unity.Notifications
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a notification to be sent or a received one.
|
||||
/// Can be converted to platform specific notification via explicit cast.
|
||||
/// <code>
|
||||
/// var n1 = (AndroidNotification)notification; // convert to Android
|
||||
/// var n1 = (iOSNotification)notification; // convert to iOS
|
||||
/// </code>
|
||||
/// </summary>
|
||||
public struct Notification
|
||||
{
|
||||
PlatformNotification notification;
|
||||
|
||||
public static explicit operator PlatformNotification(Notification n)
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
n.notification.ShowInForeground = n.ShowInForeground;
|
||||
n.notification.ShouldAutoCancel = true; // iOS always auto-cancels
|
||||
return n.notification;
|
||||
#else
|
||||
var ret = n.notification;
|
||||
if (ret != null && n.Identifier.HasValue)
|
||||
ret.Identifier = n.Identifier.Value.ToString(CultureInfo.InvariantCulture);
|
||||
ret.ShowInForeground = n.ShowInForeground;
|
||||
return ret;
|
||||
#endif
|
||||
}
|
||||
|
||||
internal Notification(PlatformNotification notification
|
||||
#if UNITY_ANDROID
|
||||
, int id
|
||||
#endif
|
||||
)
|
||||
{
|
||||
this.notification = notification;
|
||||
Identifier = default;
|
||||
ShowInForeground = notification.ShowInForeground;
|
||||
|
||||
#if UNITY_ANDROID
|
||||
Identifier = id;
|
||||
#else
|
||||
if (int.TryParse(notification.Identifier, NumberStyles.None, CultureInfo.InvariantCulture, out int val))
|
||||
Identifier = val;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !UNITY_ANDROID // so code compiles when something other than Android/iOS is selected
|
||||
PlatformNotification GetNotification()
|
||||
{
|
||||
if (notification == null)
|
||||
notification = new PlatformNotification();
|
||||
return notification;
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// A unique identifier for this notification.
|
||||
/// If null, a unique ID will be generated when scheduling.
|
||||
/// </summary>
|
||||
public int? Identifier { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// String that is shown on notification as title.
|
||||
/// </summary>
|
||||
public string Title
|
||||
{
|
||||
get
|
||||
{
|
||||
#if UNITY_IOS
|
||||
if (notification == null)
|
||||
return null;
|
||||
#endif
|
||||
return notification.Title;
|
||||
}
|
||||
set
|
||||
{
|
||||
#if UNITY_IOS
|
||||
if (notification == null)
|
||||
notification = new PlatformNotification();
|
||||
#endif
|
||||
notification.Title = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// String that is shown on notification as it's main body.
|
||||
/// </summary>
|
||||
public string Text
|
||||
{
|
||||
get
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
return notification.Text;
|
||||
#else
|
||||
return notification?.Body;
|
||||
#endif
|
||||
}
|
||||
set
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
notification.Text = value;
|
||||
#else
|
||||
GetNotification().Body = value;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Arbitrary data that is sent with notification.
|
||||
/// Can be used to store some useful information in the notification to be later retrieved when notification arrives or is tapped by user.
|
||||
/// </summary>
|
||||
public string Data
|
||||
{
|
||||
get
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
return notification.IntentData;
|
||||
#else
|
||||
return notification?.Data;
|
||||
#endif
|
||||
}
|
||||
set
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
notification.IntentData = value;
|
||||
#else
|
||||
GetNotification().Data = value;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Number, associated with the notification. Zero is ignored.
|
||||
/// When supported, shows up as badge on application launcher.
|
||||
/// </summary>
|
||||
public int Badge
|
||||
{
|
||||
get
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
return notification.Number;
|
||||
#else
|
||||
return notification == null ? 0 : notification.Badge;
|
||||
#endif
|
||||
}
|
||||
set
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
notification.Number = value;
|
||||
#else
|
||||
GetNotification().Badge = value;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// Default value differs on Android/iOS, so have separate here and unify uppon conversion
|
||||
/// <summary>
|
||||
/// Indicated, whether notification should be shown if it arrives while application is in foreground.
|
||||
/// When notification arrives with app in foreground <see cref="NotificationCenter.OnNotificationReceived"/> even fires regardless of this.
|
||||
/// Default is false, meaning notifications are silent when app is in foreground.
|
||||
/// </summary>
|
||||
public bool ShowInForeground { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Identifier for the group this notification belong to.
|
||||
/// If device supports it, notifications with same group identifier are stacked together.
|
||||
/// On Android this is also called group, while on iOS it is called thread identidier.
|
||||
/// </summary>
|
||||
public string Group
|
||||
{
|
||||
get
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
return notification.Group;
|
||||
#else
|
||||
return notification?.ThreadIdentifier;
|
||||
#endif
|
||||
}
|
||||
set
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
notification.Group = value;
|
||||
#else
|
||||
GetNotification().ThreadIdentifier = value;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marks this notification as group summary.
|
||||
/// Only has effect if <see cref="Group"/> is also set.
|
||||
/// Android only. On iOS will be just another notification in the group.
|
||||
/// </summary>
|
||||
public bool IsGroupSummary
|
||||
{
|
||||
get
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
return notification.GroupSummary;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
set
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
notification.GroupSummary = value;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3c21b2456627d40629aab46fbb054de9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+423
@@ -0,0 +1,423 @@
|
||||
using System;
|
||||
|
||||
#if UNITY_ANDROID
|
||||
using Unity.Notifications.Android;
|
||||
#else
|
||||
using System.Globalization;
|
||||
using Unity.Notifications.iOS;
|
||||
#endif
|
||||
|
||||
namespace Unity.Notifications
|
||||
{
|
||||
/// <summary>
|
||||
/// Options, specifying how notifications should be presented to the user.
|
||||
/// These option can be bitwise-ored to combine them.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// On Android Alert and Sound flags are used to choose importance level for the channel, so specifying Alert also includes sound.
|
||||
/// </remarks>
|
||||
/// <seealso cref="NotificationCenterArgs.PresentationOptions"/>
|
||||
[Flags]
|
||||
public enum NotificationPresentation
|
||||
{
|
||||
/// <summary>
|
||||
/// Specifies that when notification arrives, a pop-up should show up on screen.
|
||||
/// Alerts can be disabled by user in device settings. They may also be disabled by default.
|
||||
/// </summary>
|
||||
Alert = 1,
|
||||
|
||||
/// <summary>
|
||||
/// Whether notifications can set a badge on applications launcher.
|
||||
/// </summary>
|
||||
Badge = 1 << 1,
|
||||
|
||||
/// <summary>
|
||||
/// Whether notifications cause device to play sound uppon arrival.
|
||||
/// </summary>
|
||||
Sound = 1 << 2,
|
||||
|
||||
/// <summary>
|
||||
/// Causes device to vibrate when notification is received. Android only.
|
||||
/// </summary>
|
||||
Vibrate = 1 << 3,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The settings section to open, if possible.
|
||||
/// </summary>
|
||||
public enum NotificationSettingsSection
|
||||
{
|
||||
/// <summary>
|
||||
/// Opens settings for application and tries to open the section for notifications.
|
||||
/// </summary>
|
||||
Application,
|
||||
|
||||
/// <summary>
|
||||
/// Tries to navigate to section for a particular notification category.
|
||||
/// Since Android 8.0 will open notification settings for the specific notification channel.
|
||||
/// </summary>
|
||||
Category,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialization arguments for <see cref="NotificationCenter"/>.
|
||||
/// Recommended to use <see cref="Default"/> to retrieve recommened default values and then alter it.
|
||||
/// It is required to manually set <see cref="AndroidChannelId"/>.
|
||||
/// </summary>
|
||||
public struct NotificationCenterArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns recommended default values for all settings.
|
||||
/// </summary>
|
||||
public static NotificationCenterArgs Default => new NotificationCenterArgs()
|
||||
{
|
||||
PresentationOptions = NotificationPresentation.Badge | NotificationPresentation.Sound,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Options specifying how notifications should be presented to user when they arrive.
|
||||
/// On Android these only have effect if notification channel is created uppon initialization (all channel related properties are set).
|
||||
/// On iOS these only have effect if permission to post notification is later requested using <see cref="NotificationCenter.RequestPermission"/>
|
||||
/// </summary>
|
||||
public NotificationPresentation PresentationOptions { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A custom non-empty string to identify notification channel. Required, Android only.
|
||||
/// All notifications will be sent to this channel.
|
||||
/// Channel is created automatically during initialization if <see cref="AndroidChannelName"/> and <see cref="AndroidChannelDescription"/> are both set.
|
||||
/// If name and description are left null, channel with given identifier has to be created manually (for example using <see cref="AndroidNotificationCenter.RegisterNotificationChannel(AndroidNotificationChannel)"/>).
|
||||
/// </summary>
|
||||
public string AndroidChannelId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A user visible name for notification channel. Optional, Android only.
|
||||
/// Leave null, if you wish to create channel manually.
|
||||
/// Set this to channel name for it to be created automatically during initialization.
|
||||
/// If this is set, then <see cref="AndroidChannelDescription"/> must also be set.
|
||||
/// </summary>
|
||||
/// <seealso cref="AndroidChannelId"/>
|
||||
public string AndroidChannelName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A user visible description for the channel. Optional, Android only.
|
||||
/// Leave null, if you wish to create channel manually.
|
||||
/// Set this to channel description for it to be created automatically during initialization.
|
||||
/// If this is set, then <see cref="AndroidChannelName"/> must also be set.
|
||||
/// </summary>
|
||||
/// <seealso cref="AndroidChannelId"/>
|
||||
public string AndroidChannelDescription { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send, receive and manage notifications.
|
||||
/// Must be initialized before use. See <see cref="Initialize(NotificationCenterArgs)"/>.
|
||||
/// </summary>
|
||||
public static class NotificationCenter
|
||||
{
|
||||
/// <summary>
|
||||
/// Delegate for <see cref="OnNotificationReceived"/>.
|
||||
/// </summary>
|
||||
/// <param name="notification">Notification that has been received.</param>
|
||||
public delegate void NotificationReceivedCallback(Notification notification);
|
||||
|
||||
static bool s_Initialized = false;
|
||||
static NotificationCenterArgs s_Args;
|
||||
static event NotificationReceivedCallback s_OnNotificationReceived;
|
||||
static bool s_OnNotificationReceivedSet = false;
|
||||
|
||||
#if UNITY_ANDROID
|
||||
static void NotificationReceived(AndroidNotificationIntentData data)
|
||||
#else
|
||||
static void NotificationReceived(iOSNotification notif)
|
||||
#endif
|
||||
{
|
||||
if (s_OnNotificationReceived != null)
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
var notification = new Notification(data.Notification, data.Id);
|
||||
#else
|
||||
var notification = new Notification(notif);
|
||||
#endif
|
||||
|
||||
s_OnNotificationReceived(notification);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An event that fires when notification is received.
|
||||
/// Application must be running for this event to work.
|
||||
/// On Android this even fires if application is in foreground or when it's brought back from background.
|
||||
/// On iOS this even only fires when notification is received while app is in foreground.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This event is the same as AndroidNotificationCenter.OnNotificationReceived and iOSNotificationCenter.OnNotificationReceived.
|
||||
/// </remarks>
|
||||
public static event NotificationReceivedCallback OnNotificationReceived
|
||||
{
|
||||
add
|
||||
{
|
||||
if (!s_OnNotificationReceivedSet)
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
AndroidNotificationCenter.OnNotificationReceived += NotificationReceived;
|
||||
#else
|
||||
iOSNotificationCenter.OnNotificationReceived += NotificationReceived;
|
||||
#endif
|
||||
s_OnNotificationReceivedSet = true;
|
||||
}
|
||||
s_OnNotificationReceived += value;
|
||||
}
|
||||
remove
|
||||
{
|
||||
s_OnNotificationReceived -= value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes notification center with given arguments.
|
||||
/// On Android it will also create notification channel if arguments are set accordingly.
|
||||
/// </summary>
|
||||
/// <param name="args">Arguments for initialization.</param>
|
||||
public static void Initialize(NotificationCenterArgs args)
|
||||
{
|
||||
if (s_Initialized)
|
||||
return;
|
||||
if (string.IsNullOrEmpty(args.AndroidChannelId))
|
||||
throw new ArgumentException("AndroidChannel not provided");
|
||||
|
||||
s_Args = args;
|
||||
s_Initialized = true;
|
||||
|
||||
#if UNITY_ANDROID
|
||||
AndroidNotificationCenter.Initialize();
|
||||
if (args.AndroidChannelName != null || args.AndroidChannelDescription != null)
|
||||
{
|
||||
Importance importance = Importance.Low;
|
||||
if (0 != (args.PresentationOptions & NotificationPresentation.Alert))
|
||||
importance = Importance.High;
|
||||
else if (0 != (args.PresentationOptions & NotificationPresentation.Sound))
|
||||
importance = Importance.Default;
|
||||
|
||||
AndroidNotificationCenter.RegisterNotificationChannel(new AndroidNotificationChannel()
|
||||
{
|
||||
Id = args.AndroidChannelId,
|
||||
Name = args.AndroidChannelName,
|
||||
Description = args.AndroidChannelDescription,
|
||||
Importance = importance,
|
||||
CanShowBadge = 0 != (args.PresentationOptions & NotificationPresentation.Badge),
|
||||
EnableVibration = 0 != (args.PresentationOptions & NotificationPresentation.Vibrate),
|
||||
});
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Request for users permission to post notifications.
|
||||
/// Requests users permission to send notifications. Unless already allowed or permanently denied, shows UI to the user.
|
||||
/// Users can revoke permission in Settings while app is not running.
|
||||
/// </summary>
|
||||
/// <returns>An object for tracking the request and obtaining results.</returns>
|
||||
/// <remarks>Before Android 13 no permission is required.</remarks>
|
||||
public static NotificationsPermissionRequest RequestPermission()
|
||||
{
|
||||
CheckInitialized();
|
||||
|
||||
int iOSAuthorizationOptions = 0;
|
||||
#if UNITY_IOS
|
||||
if (0 != (s_Args.PresentationOptions & NotificationPresentation.Alert))
|
||||
iOSAuthorizationOptions |= (int)AuthorizationOption.Alert;
|
||||
if (0 != (s_Args.PresentationOptions & NotificationPresentation.Badge))
|
||||
iOSAuthorizationOptions |= (int)AuthorizationOption.Badge;
|
||||
if (0 != (s_Args.PresentationOptions & NotificationPresentation.Sound))
|
||||
iOSAuthorizationOptions |= (int)AuthorizationOption.Sound;
|
||||
#endif
|
||||
return new NotificationsPermissionRequest(iOSAuthorizationOptions);
|
||||
}
|
||||
|
||||
static void CheckInitialized()
|
||||
{
|
||||
if (!s_Initialized)
|
||||
throw new Exception("NotificationCenter not initialized");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Schedule notification to be shown in the future.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the schedule, usually deduced from actually passed one.</typeparam>
|
||||
/// <param name="notification">Notification to send.</param>
|
||||
/// <param name="schedule">Schedule, specifying, when notification should be shown.</param>
|
||||
/// <returns>Notification identifier.</returns>
|
||||
public static int ScheduleNotification<T>(Notification notification, T schedule)
|
||||
where T : NotificationSchedule
|
||||
{
|
||||
string category = null;
|
||||
#if UNITY_ANDROID
|
||||
category = s_Args.AndroidChannelId;
|
||||
#endif
|
||||
return ScheduleNotification(notification, category, schedule);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Schedule notification to be shown in the future.
|
||||
/// Allows to explicitly specify the category to send notification to. On Android it is notification channel.
|
||||
/// Channel or category has to be created manually using AndroidNotificationCenter and iOSNotificationCenter respectively.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the schedule, usually deduced from actually passed one.</typeparam>
|
||||
/// <param name="notification">Notification to send.</param>
|
||||
/// <param name="category">Identifier for iOS category or Android channel.</param>
|
||||
/// <param name="schedule">Schedule, specifying, when notification should be shown.</param>
|
||||
/// <returns>Notification identifier.</returns>
|
||||
public static int ScheduleNotification<T>(Notification notification, string category, T schedule)
|
||||
where T : NotificationSchedule
|
||||
{
|
||||
CheckInitialized();
|
||||
|
||||
#if UNITY_ANDROID
|
||||
var n = (AndroidNotification)notification;
|
||||
schedule.Schedule(ref n);
|
||||
if (notification.Identifier.HasValue)
|
||||
{
|
||||
AndroidNotificationCenter.SendNotificationWithExplicitID(n, category, notification.Identifier.Value);
|
||||
return notification.Identifier.Value;
|
||||
}
|
||||
else
|
||||
return AndroidNotificationCenter.SendNotification(n, category);
|
||||
#else
|
||||
var n = (iOSNotification)notification;
|
||||
if (n == null)
|
||||
throw new ArgumentException("Passed notifiation is empty");
|
||||
n.CategoryIdentifier = category;
|
||||
schedule.Schedule(ref n);
|
||||
iOSNotificationCenter.ScheduleNotification(n);
|
||||
if (notification.Identifier.HasValue)
|
||||
return notification.Identifier.Value;
|
||||
// iOSNotification is class and has auto-generated id set at this point
|
||||
// for consistency with Android set it back to null, so same Notification can be sent again as new one
|
||||
int id = int.Parse(n.Identifier, NumberStyles.None, CultureInfo.InvariantCulture);
|
||||
n.Identifier = null;
|
||||
return id;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns last notification tapped by user, or null.
|
||||
/// </summary>
|
||||
public static Notification? LastRespondedNotification
|
||||
{
|
||||
get
|
||||
{
|
||||
CheckInitialized();
|
||||
|
||||
#if UNITY_ANDROID
|
||||
var intent = AndroidNotificationCenter.GetLastNotificationIntent();
|
||||
if (intent == null)
|
||||
return null;
|
||||
return new Notification(intent.Notification, intent.Id);
|
||||
#else
|
||||
var notification = iOSNotificationCenter.GetLastRespondedNotification();
|
||||
if (notification == null)
|
||||
return null;
|
||||
return new Notification(notification);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cancel a scheduled notification.
|
||||
/// </summary>
|
||||
/// <param name="id">ID of the notification to cancel.</param>
|
||||
public static void CancelScheduledNotification(int id)
|
||||
{
|
||||
CheckInitialized();
|
||||
|
||||
#if UNITY_ANDROID
|
||||
AndroidNotificationCenter.CancelScheduledNotification(id);
|
||||
#else
|
||||
iOSNotificationCenter.RemoveScheduledNotification(id.ToString(CultureInfo.InvariantCulture));
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cancel delivered notification.
|
||||
/// Removes notification from the tray.
|
||||
/// </summary>
|
||||
/// <param name="id">ID of the notification to cancel.</param>
|
||||
public static void CancelDeliveredNotification(int id)
|
||||
{
|
||||
CheckInitialized();
|
||||
|
||||
#if UNITY_ANDROID
|
||||
AndroidNotificationCenter.CancelDisplayedNotification(id);
|
||||
#else
|
||||
iOSNotificationCenter.RemoveDeliveredNotification(id.ToString(CultureInfo.InvariantCulture));
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cancel all future notifications.
|
||||
/// </summary>
|
||||
public static void CancelAllScheduledNotifications()
|
||||
{
|
||||
CheckInitialized();
|
||||
|
||||
#if UNITY_ANDROID
|
||||
AndroidNotificationCenter.CancelAllScheduledNotifications();
|
||||
#else
|
||||
iOSNotificationCenter.RemoveAllScheduledNotifications();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove all already delivered notifications.
|
||||
/// </summary>
|
||||
public static void CancelAllDeliveredNotifications()
|
||||
{
|
||||
CheckInitialized();
|
||||
|
||||
#if UNITY_ANDROID
|
||||
AndroidNotificationCenter.CancelAllDisplayedNotifications();
|
||||
#else
|
||||
iOSNotificationCenter.RemoveAllDeliveredNotifications();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clear Application badge. iOS only.
|
||||
/// iOS applications can set numeric badge on app icon. Calling this method removes that badge.
|
||||
/// On Android badge is removed automatically when notifications are removed.
|
||||
/// </summary>
|
||||
public static void ClearBadge()
|
||||
{
|
||||
CheckInitialized();
|
||||
|
||||
#if UNITY_IOS
|
||||
iOSNotificationCenter.ApplicationBadge = 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens settings for the application.
|
||||
/// If possible, will try to navigate as close to requested section as it can.
|
||||
/// On iOS and Android prior to 8.0 will open settings for the application.
|
||||
/// Since Android 8.0 will open notification settings for either application or the default channel.
|
||||
/// </summary>
|
||||
/// <param name="section">The section to navigate to.</param>
|
||||
public static void OpenNotificationSettings(NotificationSettingsSection section = NotificationSettingsSection.Application)
|
||||
{
|
||||
CheckInitialized();
|
||||
|
||||
#if UNITY_ANDROID
|
||||
string channel = section switch
|
||||
{
|
||||
NotificationSettingsSection.Category => s_Args.AndroidChannelId,
|
||||
NotificationSettingsSection.Application => null,
|
||||
_ => null,
|
||||
};
|
||||
|
||||
AndroidNotificationCenter.OpenNotificationSettings(channel);
|
||||
#else
|
||||
iOSNotificationCenter.OpenNotificationSettings();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 89f50368f285b453ebeac8f965391651
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+158
@@ -0,0 +1,158 @@
|
||||
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
|
||||
{
|
||||
/// <summary>
|
||||
/// Interval, at which notification should repeat.
|
||||
/// </summary>
|
||||
public enum NotificationRepeatInterval
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates, that notification does not repeat.
|
||||
/// </summary>
|
||||
OneTime = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates, that notification should repeat daily.
|
||||
/// When used in <see cref="NotificationDateTimeSchedule"/>, only time is used for scheduling.
|
||||
/// </summary>
|
||||
Daily = 1,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marker interface for different schedule types.
|
||||
/// </summary>
|
||||
public interface NotificationSchedule
|
||||
{
|
||||
internal void Schedule(ref PlatformNotification notification);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Schedule notification to show up after a certain amount of time, optionally repeating at the same time interval.
|
||||
/// </summary>
|
||||
public struct NotificationIntervalSchedule
|
||||
: NotificationSchedule
|
||||
{
|
||||
/// <summary>
|
||||
/// Time interval to show notification from current time.
|
||||
/// Only full seconds are considered.
|
||||
/// </summary>
|
||||
public TimeSpan Interval { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether notification should repeat.
|
||||
/// If true, notification will repeat at the same interval as initial time from the current one.
|
||||
/// </summary>
|
||||
public bool Repeats { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Convenience constructor.
|
||||
/// </summary>
|
||||
/// <param name="interval">Value for <see cref="Interval"/></param>
|
||||
/// <param name="repeats">Value for <see cref="Repeats"/></param>
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Schedule to show notification at particular date and time.
|
||||
/// Optionally can repeat at predefined intervals.
|
||||
/// </summary>
|
||||
public struct NotificationDateTimeSchedule
|
||||
: NotificationSchedule
|
||||
{
|
||||
/// <summary>
|
||||
/// 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 <see cref="NotificationRepeatInterval"/>.
|
||||
/// </summary>
|
||||
public DateTime FireTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Interval, at which notification should repeat from the first delivery.
|
||||
/// </summary>
|
||||
public NotificationRepeatInterval RepeatInterval { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Convenience constructor.
|
||||
/// </summary>
|
||||
/// <param name="fireTime">Value for <see cref="FireTime"/></param>
|
||||
/// <param name="repeatInterval">Value for <see cref="RepeatInterval"/></param>
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 304ff37de66064f79ad81cb7991a4b36
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
using UnityEngine;
|
||||
|
||||
#if UNITY_ANDROID
|
||||
using Unity.Notifications.Android;
|
||||
#else
|
||||
using Unity.Notifications.iOS;
|
||||
#endif
|
||||
|
||||
namespace Unity.Notifications
|
||||
{
|
||||
/// <summary>
|
||||
/// The status of notification permission request.
|
||||
/// </summary>
|
||||
/// <seealso cref="NotificationsPermissionRequest.Status"/>
|
||||
public enum NotificationsPermissionStatus
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates that request is ongoing. Usually mean that user is presented with UI to respond to.
|
||||
/// </summary>
|
||||
RequestPending,
|
||||
|
||||
/// <summary>
|
||||
/// Permission granted, you can post notifications.
|
||||
/// </summary>
|
||||
Granted,
|
||||
|
||||
/// <summary>
|
||||
/// Permission denied, sending notifications will not work.
|
||||
/// </summary>
|
||||
Denied,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Track the status of notification permission request.
|
||||
/// Can be returned from coroutine to suspend it until request is either granted or denied.
|
||||
/// Permission can be granted or denied immediately, if this isn't the first request.
|
||||
/// </summary>
|
||||
public class NotificationsPermissionRequest
|
||||
: CustomYieldInstruction
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
PermissionRequest request;
|
||||
#else
|
||||
AuthorizationRequest request;
|
||||
#endif
|
||||
|
||||
internal NotificationsPermissionRequest(int options)
|
||||
{
|
||||
#if UNITY_ANDROID
|
||||
// do not create request if already allowed
|
||||
if (AndroidNotificationCenter.UserPermissionToPost != PermissionStatus.Allowed)
|
||||
request = new PermissionRequest();
|
||||
#else
|
||||
request = new AuthorizationRequest((AuthorizationOption)options, false);
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Overridden property of base class. Indicates if coroutine should be suspended.
|
||||
/// </summary>
|
||||
public override bool keepWaiting => (request == null)
|
||||
? false
|
||||
#if UNITY_ANDROID
|
||||
: request.Status == PermissionStatus.RequestPending;
|
||||
#else
|
||||
: !request.IsFinished;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// Returns a status of this request.
|
||||
/// </summary>
|
||||
public NotificationsPermissionStatus Status
|
||||
{
|
||||
get
|
||||
{
|
||||
if (request == null)
|
||||
return NotificationsPermissionStatus.Granted;
|
||||
|
||||
#if UNITY_ANDROID
|
||||
return request.Status switch
|
||||
{
|
||||
PermissionStatus.RequestPending => NotificationsPermissionStatus.RequestPending,
|
||||
PermissionStatus.Allowed => NotificationsPermissionStatus.Granted,
|
||||
_ => NotificationsPermissionStatus.Denied,
|
||||
};
|
||||
#else
|
||||
if (!request.IsFinished)
|
||||
return NotificationsPermissionStatus.RequestPending;
|
||||
return request.Granted ? NotificationsPermissionStatus.Granted : NotificationsPermissionStatus.Denied;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cf48a9bb20f8b4cdbbf92393fd680077
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"name": "Unity.Notifications.Unified",
|
||||
"rootNamespace": "",
|
||||
"references": [
|
||||
"Unity.Notifications.Android",
|
||||
"Unity.Notifications.iOS"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Android",
|
||||
"Editor",
|
||||
"iOS"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": false,
|
||||
"overrideReferences": false,
|
||||
"precompiledReferences": [],
|
||||
"autoReferenced": true,
|
||||
"defineConstraints": [],
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d71139be5ab9e4bb49bc95f8093014e9
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user