Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
IosNotificationRenderer.cs
2using System.Text.Json;
3using System.Threading;
4using System.Threading.Tasks;
5using Foundation;
7using UserNotifications;
8
10{
15 {
21 public async Task RenderAsync(NotificationIntent Intent, CancellationToken CancellationToken)
22 {
23 CancellationToken.ThrowIfCancellationRequested();
24
25 if (Intent.Presentation is NotificationPresentation.StoreOnly or NotificationPresentation.Transient)
26 return;
27
28 UNMutableNotificationContent content = new()
29 {
30 Title = Intent.Title,
31 Body = Intent.Body ?? string.Empty,
32 Sound = UNNotificationSound.Default,
33 UserInfo = this.BuildUserInfo(Intent)
34 };
35
36 UNNotificationRequest request = UNNotificationRequest.FromIdentifier(
37 Guid.NewGuid().ToString(),
38 content,
39 trigger: null);
40
41
42 await UNUserNotificationCenter.Current.AddNotificationRequestAsync(request);
43 }
44
45 private NSDictionary BuildUserInfo(NotificationIntent Intent)
46 {
47 NSMutableDictionary dictionary = new();
48
49 if (!string.IsNullOrEmpty(Intent.Channel))
50 dictionary.SetValueForKey(new NSString(Intent.Channel), new NSString("channelId"));
51
52 dictionary.SetValueForKey(new NSString(Intent.Title), new NSString("myTitle"));
53
54 if (!string.IsNullOrEmpty(Intent.Body))
55 dictionary.SetValueForKey(new NSString(Intent.Body), new NSString("myBody"));
56
57 dictionary.SetValueForKey(new NSString(Intent.Action.ToString()), new NSString("action"));
58
59 if (!string.IsNullOrEmpty(Intent.EntityId))
60 dictionary.SetValueForKey(new NSString(Intent.EntityId), new NSString("entityId"));
61
62 if (!string.IsNullOrEmpty(Intent.CorrelationId))
63 dictionary.SetValueForKey(new NSString(Intent.CorrelationId), new NSString("correlationId"));
64
65 foreach (KeyValuePair<string, string> pair in Intent.Extras)
66 {
67 dictionary.SetValueForKey(new NSString(pair.Value), new NSString(pair.Key));
68 }
69
70 try
71 {
72 string json = JsonSerializer.Serialize(Intent);
73 dictionary.SetValueForKey(new NSString(json), new NSString("notificationIntent"));
74 }
75 catch
76 {
77 // If serialization fails, continue without the aggregated payload.
78 }
79
80 return dictionary;
81 }
82 }
83}
Platform-neutral intent describing how to route a notification.
string? EntityId
Gets or sets an entity identifier associated with the action.
string? CorrelationId
Gets or sets an optional correlation identifier.
string? Body
Gets or sets the message body to display.
string Title
Gets or sets the title to display.
NotificationPresentation Presentation
Gets or sets how the notification should be presented.
NotificationAction Action
Gets or sets the intended action.
string? Channel
Gets or sets the push channel identifier.
Dictionary< string, string > Extras
Gets or sets extra data used for routing.
async Task RenderAsync(NotificationIntent Intent, CancellationToken CancellationToken)
Displays a local notification with the provided intent.
Renders local notifications on the platform.
NotificationPresentation
Presentation preference for a notification intent.