Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NotificationDelegate.cs
1using System;
3using System.Text.Json;
4using System.Threading;
5using Firebase.CloudMessaging;
6using Foundation;
9using UIKit;
10using UserNotifications;
11
12namespace NeuroAccessMaui
13{
14 public class NotificationDelegate : UNUserNotificationCenterDelegate
15 {
16 // Called when a notification is delivered while the app is in the foreground.
17 [Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
18 public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
19 {
20 var userInfo = notification.Request.Content.UserInfo;
21
22 Messaging.SharedInstance.AppDidReceiveMessage(userInfo);
23
24 Console.WriteLine("Foreground notification received: " + userInfo);
25
26 try
27 {
28 NotificationIntent? intent = this.ParseIntent(userInfo);
29 if (intent is not null)
30 {
32 string raw = JsonSerializer.Serialize(userInfo);
33 service.AddAsync(intent, NotificationSource.Push, raw, CancellationToken.None).ConfigureAwait(false);
34 }
35 }
36 catch (Exception ex)
37 {
38 ServiceRef.LogService.LogException(ex);
39 }
40
41 // Suppress foreground banners; store silently.
42 completionHandler(UNNotificationPresentationOptions.None);
43 }
44
45 // Called when the user interacts with a notification (e.g., taps it).
46 [Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
47 public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
48 {
49 var userInfo = response.Notification.Request.Content.UserInfo;
50 Console.WriteLine("Notification tapped with data: " + userInfo);
51
52 try
53 {
54 NotificationIntent? intent = this.ParseIntent(userInfo);
55 if (intent is not null)
56 {
58 string raw = JsonSerializer.Serialize(userInfo);
59 service.AddAsync(intent, NotificationSource.Push, raw, CancellationToken.None).ConfigureAwait(false);
60 string id = service.ComputeId(intent, NotificationSource.Push);
61 service.ConsumeAsync(id, CancellationToken.None).ConfigureAwait(false);
62 }
63 }
64 catch (Exception ex)
65 {
66 ServiceRef.LogService.LogException(ex);
67 }
68
69 completionHandler();
70 }
71
72 public static void ProcessSilentNotification(NSDictionary userInfo)
73 {
74 try
75 {
76 NotificationIntent? intent = ParseIntentStatic(userInfo);
77 if (intent is null)
78 return;
79
80 string raw = JsonSerializer.Serialize(userInfo);
82 service.AddAsync(intent, NotificationSource.Push, raw, CancellationToken.None).ConfigureAwait(false);
83
84 }
85 catch (Exception ex)
86 {
87 ServiceRef.LogService.LogException(ex);
88 }
89 }
90
91 private NotificationIntent? ParseIntent(NSDictionary userInfo) => ParseIntentStatic(userInfo);
92
93 private static NotificationIntent? ParseIntentStatic(NSDictionary userInfo)
94 {
95 try
96 {
97 if (userInfo["notificationIntent"] is NSString intentJson)
98 {
99 NotificationIntent? parsedNotification = JsonSerializer.Deserialize<NotificationIntent>(intentJson.ToString());
100 if (parsedNotification is not null)
101 return parsedNotification;
102 }
103
104 string? channelId = userInfo["channelId"]?.ToString();
105 string? title = userInfo["myTitle"]?.ToString();
106 string? body = userInfo["myBody"]?.ToString();
107
108 if (channelId is null)
109 return null;
110
111 NotificationIntent intent = new()
112 {
113 Channel = channelId,
114 Title = title ?? string.Empty,
115 Body = body
116 };
117
118 foreach (NSObject key in userInfo.Keys)
119 {
120 string k = key.ToString();
121 string v = userInfo[key]?.ToString() ?? string.Empty;
122 intent.Extras[k] = v;
123 }
124
125 if (intent.Extras.TryGetValue("action", out string? action) && Enum.TryParse(action, out NotificationAction parsed))
126 intent.Action = parsed;
127
128 if (intent.Extras.TryGetValue("entityId", out string? entityId))
129 intent.EntityId = entityId;
130
131 if (intent.Extras.TryGetValue("correlationId", out string? correlationId))
132 intent.CorrelationId = correlationId;
133
134 intent.Presentation = ResolvePresentation(intent.Extras);
135
136 return intent;
137 }
138 catch
139 {
140 return null;
141 }
142 }
143
144 private static NotificationPresentation ResolvePresentation(Dictionary<string, string> extras)
145 {
146 if (extras.TryGetValue("silent", out string silent) && IsTrue(silent))
147 return NotificationPresentation.StoreOnly;
148
149 if (extras.TryGetValue("delivery.silent", out string deliverySilent) && IsTrue(deliverySilent))
150 return NotificationPresentation.StoreOnly;
151
152 return NotificationPresentation.RenderAndStore;
153 }
154
155 private static bool IsTrue(string value)
156 {
157 return value.Equals("1", StringComparison.OrdinalIgnoreCase) ||
158 value.Equals("true", StringComparison.OrdinalIgnoreCase) ||
159 value.Equals("yes", StringComparison.OrdinalIgnoreCase);
160 }
161 }
162}
Platform-neutral intent describing how to route a notification.
Dictionary< string, string > Extras
Gets or sets extra data used for routing.
Base class that references services in the app.
Definition: ServiceRef.cs:43
static IServiceProvider Provider
The service provider for the app. This is set before the app is started, and will be used to resolve ...
Definition: ServiceRef.cs:48
static ILogService LogService
Log service.
Definition: ServiceRef.cs:214
Interface for the redesigned notification service.
string ComputeId(NotificationIntent Intent, NotificationSource Source)
Computes the stable identifier for an intent using the same logic as storage.
Task AddAsync(NotificationIntent Intent, NotificationSource Source, string? RawPayload, CancellationToken CancellationToken)
Adds or updates a notification based on the provided intent.
Task ConsumeAsync(string Id, CancellationToken CancellationToken)
Marks a notification as consumed and routes it.
NotificationAction
Actions that can be routed from notifications.
NotificationPresentation
Presentation preference for a notification intent.
NotificationSource
Describes the source producing a notification.