Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NotificationPermission.cs
1#if IOS
2using Foundation;
3using UIKit;
4using UserNotifications;
5#endif
6#if ANDROID
7using System.Linq;
8using Android;
9using Android.App;
10using Android.Content.PM;
11using Android.OS;
12using AndroidX.Core.Content;
14#endif
15
16
18{
19 public class NotificationPermission : Permissions.BasePermission
20 {
21 public override Task<PermissionStatus> CheckStatusAsync()
22 {
23#if IOS
24 return CheckStatusIOSAsync();
25#elif ANDROID
26 // For Android 13+ (API level 33), notifications are a runtime permission.
27 if (Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu && OperatingSystem.IsAndroidVersionAtLeast(33))
28 {
29 Android.Content.Context Context = Android.App.Application.Context;
30 // Check the POST_NOTIFICATIONS permission
31 int Result = (int)ContextCompat.CheckSelfPermission(Context, Manifest.Permission.PostNotifications);
32 return Task.FromResult(Result == (int)Permission.Granted ? PermissionStatus.Granted : PermissionStatus.Denied);
33 }
34 // For earlier versions, notifications are granted by default.
35 return Task.FromResult(PermissionStatus.Granted);
36#else
37 return Task.FromResult(PermissionStatus.Unknown);
38#endif
39 }
40
41#if IOS
42 private static async Task<PermissionStatus> CheckStatusIOSAsync()
43 {
44 // Check the current notification settings on iOS
45 UNNotificationSettings Settings = await UNUserNotificationCenter.Current.GetNotificationSettingsAsync();
46 return Settings.AuthorizationStatus switch
47 {
48 UNAuthorizationStatus.Authorized => PermissionStatus.Granted,
49 UNAuthorizationStatus.Denied => PermissionStatus.Denied,
50 _ => PermissionStatus.Unknown
51 };
52 }
53#endif
54
55 public override void EnsureDeclared()
56 {
57#if ANDROID
58 if (!OperatingSystem.IsAndroidVersionAtLeast(33))
59 return;
60
61 try
62 {
63 // On Android, check that the POST_NOTIFICATIONS permission is declared in the manifest.
64 Android.Content.Context Context = Android.App.Application.Context;
65 PackageManager PackageManager = Context.PackageManager ?? throw new Exception();
66 string? PackageName = Context.PackageName;
67
68 PackageInfo PackageInfo = PackageManager.GetPackageInfo(PackageName ?? string.Empty, PackageInfoFlags.Permissions) ?? throw new Exception();
69 IList<string>? DeclaredPermissions = PackageInfo.RequestedPermissions;
70 if (DeclaredPermissions is null || !DeclaredPermissions.Contains(Manifest.Permission.PostNotifications))
71 {
72 throw new PermissionException($"The Android manifest does not declare the required permission: {Manifest.Permission.PostNotifications}");
73 }
74 }
75 catch (PackageManager.NameNotFoundException)
76 {
77 ServiceRef.LogService.LogWarning("Unable to verify that the POST_NOTIFICATIONS permission is declared in the manifest.");
78 throw new PermissionException("The Android manifest does not declare the required permission: " + Manifest.Permission.PostNotifications);
79 }
80 catch (Exception)
81 {
82 // assume declared if there is any weird errors with the PackageManager
83 return;
84 }
85#endif
86 // For iOS and other platforms, no manifest declaration is required.
87 }
88
89 public override async Task<PermissionStatus> RequestAsync()
90 {
91#if IOS
92 // Request notification permission on iOS using UNUserNotificationCenter
93 (bool Granted, NSError Error) = await UNUserNotificationCenter.Current.RequestAuthorizationAsync(
94 UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound);
95
96 if (Granted)
97 {
98 UIApplication.SharedApplication.InvokeOnMainThread(() =>
99 {
100 UIApplication.SharedApplication.RegisterForRemoteNotifications();
101 });
102 }
103 return Granted ? PermissionStatus.Granted : PermissionStatus.Denied;
104#elif ANDROID
105 if (Build.VERSION.SdkInt >= BuildVersionCodes.Tiramisu && OperatingSystem.IsAndroidVersionAtLeast(33))
106 {
107 // For Android 13+, request the POST_NOTIFICATIONS permission.
108 PermissionStatus Status = await Permissions.RequestAsync<Permissions.PostNotifications>();
109 return Status;
110 }
111 // For earlier versions, notifications are automatically granted.
112 return PermissionStatus.Granted;
113#else
114 return PermissionStatus.Unknown;
115#endif
116 }
117
118 // Optional: Override ShouldShowRationale if you want to provide additional info to the user.
119 public override bool ShouldShowRationale() => true;
120 }
121}
Base class that references services in the app.
Definition: ServiceRef.cs:43
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
Definition: ImplTypes.g.cs:58