Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions Source/Plugin.LocalNotification/Apple/ApplePlatform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#if IOS || MACCATALYST
using System.Runtime.Versioning;

namespace Plugin.LocalNotification;

internal static class ApplePlatform
{
internal static bool IsCurrent() =>
#if IOS
OperatingSystem.IsIOS();
#else
OperatingSystem.IsMacCatalyst();
#endif

#if IOS
[SupportedOSPlatformGuard("ios11.0")]
#else
[SupportedOSPlatformGuard("maccatalyst11.0")]
#endif
internal static bool IsVersion11OrLater() => IsVersionAtLeast(11);

#if IOS
[SupportedOSPlatformGuard("ios12.0")]
#else
[SupportedOSPlatformGuard("maccatalyst12.0")]
#endif
internal static bool IsVersion12OrLater() => IsVersionAtLeast(12);

#if IOS
[SupportedOSPlatformGuard("ios14.0")]
#else
[SupportedOSPlatformGuard("maccatalyst14.0")]
#endif
internal static bool IsVersion14OrLater() => IsVersionAtLeast(14);

#if IOS
[SupportedOSPlatformGuard("ios15.0")]
#else
[SupportedOSPlatformGuard("maccatalyst15.0")]
#endif
internal static bool IsVersion15OrLater() => IsVersionAtLeast(15);

#if IOS
[SupportedOSPlatformGuard("ios16.0")]
#else
[SupportedOSPlatformGuard("maccatalyst16.0")]
#endif
internal static bool IsVersion16OrLater() => IsVersionAtLeast(16);

private static bool IsVersionAtLeast(int majorVersion) =>
#if IOS
OperatingSystem.IsIOSVersionAtLeast(majorVersion);
#else
OperatingSystem.IsMacCatalystVersionAtLeast(majorVersion);
#endif
}
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Foundation;
#if IOS || MACCATALYST
using Foundation;
using Plugin.LocalNotification.Core;
using Plugin.LocalNotification.Core.Models;
using Plugin.LocalNotification.Platforms;
Expand Down Expand Up @@ -72,7 +73,7 @@ public static void ResetApplicationIconBadgeNumber(UIApplication uiApplication)

uiApplication.InvokeOnMainThread(() =>
{
if (OperatingSystem.IsMacCatalystVersionAtLeast(16))
if (ApplePlatform.IsVersion16OrLater())
{
UNUserNotificationCenter.Current.SetBadgeCount(0, (error) =>
{
Expand Down Expand Up @@ -115,7 +116,7 @@ public static async Task ResetApplicationIconBadgeNumberAsync(UIApplication uiAp

uiApplication.InvokeOnMainThread(async () =>
{
if (OperatingSystem.IsMacCatalystVersionAtLeast(16))
if (ApplePlatform.IsVersion16OrLater())
{
await UNUserNotificationCenter.Current.SetBadgeCountAsync(0);
}
Expand All @@ -132,4 +133,5 @@ public static async Task ResetApplicationIconBadgeNumberAsync(UIApplication uiAp
throw;
}
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using CoreGraphics;
#if IOS || MACCATALYST
using CoreGraphics;
using Foundation;
using Plugin.LocalNotification.Core;
using Plugin.LocalNotification.Core.Models;
using Plugin.LocalNotification.Core.Models.AppleOption;
#if IOS
using Plugin.LocalNotification.Core.Platforms.iOS;
#else
using Plugin.LocalNotification.Core.Platforms.MacCatalyst;
#endif
using Plugin.LocalNotification.EventArgs;
using System.Globalization;
using UIKit;
Expand All @@ -18,7 +23,7 @@ internal class NotificationServiceImpl : INotificationService
public Func<NotificationRequest, Task<NotificationEventReceivingArgs>>? NotificationReceiving { get; set; }

/// <inheritdoc />
public bool IsSupported => OperatingSystem.IsIOS();
public bool IsSupported => ApplePlatform.IsCurrent();

/// <inheritdoc />
public event NotificationReceivedEventHandler? NotificationReceived;
Expand All @@ -41,7 +46,7 @@ internal class NotificationServiceImpl : INotificationService
/// <inheritdoc />
public bool Cancel(params int[] notificationIdList)
{
if (!OperatingSystem.IsIOSVersionAtLeast(11))
if (!ApplePlatform.IsVersion11OrLater())
{
return false;
}
Expand Down Expand Up @@ -87,7 +92,7 @@ public async Task<bool> Show(NotificationRequest request)
UNNotificationTrigger? trigger = null;
try
{
if (!OperatingSystem.IsIOS())
if (!ApplePlatform.IsCurrent())
{
return false;
}
Expand Down Expand Up @@ -166,7 +171,7 @@ await UNUserNotificationCenter.Current.AddNotificationRequestAsync(nativeRequest
/// <returns></returns>
public async Task<UNMutableNotificationContent> GetNotificationContent(NotificationRequest request)
{
if (!OperatingSystem.IsIOS())
if (!ApplePlatform.IsCurrent())
{
return new UNMutableNotificationContent();
}
Expand All @@ -184,7 +189,7 @@ public async Task<UNMutableNotificationContent> GetNotificationContent(Notificat
UserInfo = userInfoDictionary
};

if (OperatingSystem.IsIOSVersionAtLeast(15))
if (ApplePlatform.IsVersion15OrLater())
{
content.InterruptionLevel = request.Apple.Priority.ToNative();
content.RelevanceScore = request.Apple.RelevanceScore;
Expand Down Expand Up @@ -212,9 +217,9 @@ public async Task<UNMutableNotificationContent> GetNotificationContent(Notificat

if (string.IsNullOrWhiteSpace(request.Apple.SummaryArgument) == false)
{
if (OperatingSystem.IsIOS() &&
OperatingSystem.IsIOSVersionAtLeast(12) &&
!OperatingSystem.IsIOSVersionAtLeast(15))
if (ApplePlatform.IsCurrent() &&
ApplePlatform.IsVersion12OrLater() &&
!ApplePlatform.IsVersion15OrLater())
{
content.SummaryArgument = request.Apple.SummaryArgument;
content.SummaryArgumentCount = (nuint)request.Apple.SummaryArgumentCount;
Expand Down Expand Up @@ -294,7 +299,7 @@ public async Task<UNMutableNotificationContent> GetNotificationContent(Notificat

private static UNNotificationSound BuildNotificationSound(string? soundName, float? criticalVolume)
{
if (criticalVolume is not null && OperatingSystem.IsIOSVersionAtLeast(12))
if (criticalVolume is not null && ApplePlatform.IsVersion12OrLater())
{
var volume = (float)Math.Clamp(criticalVolume.Value, 0.0, 1.0);
return string.IsNullOrWhiteSpace(soundName)
Expand Down Expand Up @@ -418,7 +423,7 @@ public void RegisterCategoryList(HashSet<NotificationCategory> categoryList)
var options = notificationAction.Apple.Action.ToNative();
var hasTextInput = !string.IsNullOrEmpty(notificationAction.Apple.TextInputButtonTitle);

if (OperatingSystem.IsIOSVersionAtLeast(15))
if (ApplePlatform.IsVersion15OrLater())
{
var icon = notificationAction.Apple.Icon.Type switch
{
Expand Down Expand Up @@ -584,4 +589,5 @@ public async Task<bool> RequestNotificationPermission(NotificationPermission? pe
return false;
}
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Plugin.LocalNotification.Core;
#if IOS || MACCATALYST
using Plugin.LocalNotification.Core;
using Plugin.LocalNotification.EventArgs;
using System.Globalization;
using UIKit;
Expand Down Expand Up @@ -196,7 +197,7 @@ public override void WillPresentNotification(UNUserNotificationCenter center, UN

if (requestHandled == false)
{
if (OperatingSystem.IsIOSVersionAtLeast(14))
if (ApplePlatform.IsVersion14OrLater())
{
if (notificationRequest.Apple.PresentAsBanner)
{
Expand Down Expand Up @@ -249,4 +250,5 @@ public override void WillPresentNotification(UNUserNotificationCenter center, UN
internal static NotificationServiceImpl TryGetDefaultIOsNotificationService() => LocalNotificationCenter.Current is NotificationServiceImpl notificationService
? notificationService
: new NotificationServiceImpl();
}
}
#endif
Loading
Loading