Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AndroidNotificationRenderer.cs
2using System.Threading;
3using System.Threading.Tasks;
4using Android.App;
5using Android.Content;
6using Android.OS;
7using AndroidX.Core.App;
8using AndroidNotification = Android.App.Notification;
10using System.Text.Json;
11using System;
12using NeuroAccessMaui;
13
15{
20 {
21 private const string DefaultChannelId = "default";
22 private readonly HashSet<string> createdChannels = new HashSet<string>();
23
30 public Task RenderAsync(NotificationIntent NotificationIntent, CancellationToken CancellationToken)
31 {
32 ArgumentNullException.ThrowIfNull(NotificationIntent);
33 CancellationToken.ThrowIfCancellationRequested();
34
36 {
37 return Task.CompletedTask;
38 }
39
40 Context? ApplicationContext = Android.App.Application.Context;
41 if (ApplicationContext is null)
42 {
43 return Task.CompletedTask;
44 }
45 Context ApplicationContextInstance = ApplicationContext;
46
47 string ChannelId = string.IsNullOrWhiteSpace(NotificationIntent.Channel) ? DefaultChannelId : NotificationIntent.Channel!;
48
49 NotificationManager? NotificationManagerInstance = ApplicationContextInstance.GetSystemService(Context.NotificationService) as NotificationManager;
50 if (NotificationManagerInstance is null)
51 {
52 return Task.CompletedTask;
53 }
54 NotificationManager NotificationManager = NotificationManagerInstance;
55
56 this.EnsureChannel(NotificationManager, ChannelId);
57
58 int IconId = ApplicationContextInstance.Resources?.GetIdentifier(
59 "ic_stat_appiconfg",
60 "drawable",
61 ApplicationContextInstance.PackageName) ?? ApplicationContextInstance.ApplicationInfo?.Icon ?? Android.Resource.Drawable.SymDefAppIcon;
62 NotificationCompat.Builder? Builder = new NotificationCompat.Builder(ApplicationContextInstance, ChannelId)
63 .SetContentTitle(NotificationIntent.Title ?? string.Empty)?
64 .SetContentText(NotificationIntent.Body ?? string.Empty)?
65 .SetSmallIcon(IconId)?
66 .SetAutoCancel(true)?
67 .SetPriority((int)NotificationPriority.High);
68
69 if (OperatingSystem.IsAndroidVersionAtLeast(26))
70 {
71 Builder?.SetCategory(AndroidNotification.CategoryMessage);
72 }
73
74 try
75 {
76 string Payload = JsonSerializer.Serialize(NotificationIntent);
77 Intent LaunchIntent = new Intent(ApplicationContextInstance, typeof(MainActivity));
78 LaunchIntent.SetAction(Intent.ActionView);
79 LaunchIntent.PutExtra("notificationIntent", Payload);
80 LaunchIntent.AddFlags(ActivityFlags.SingleTop | ActivityFlags.ClearTop);
81
82 int RequestCode = Math.Abs(Payload.GetHashCode());
83 PendingIntent? PendingIntentInstance = PendingIntent.GetActivity(
84 ApplicationContextInstance,
85 RequestCode,
86 LaunchIntent,
87 PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable | PendingIntentFlags.OneShot);
88
89 if (PendingIntentInstance is not null)
90 {
91 Builder?.SetContentIntent(PendingIntentInstance);
92 }
93 }
94 catch (Exception)
95 {
96 // If serialization fails, fall back to a notification without tap handling.
97 }
98
99 if (Builder is null)
100 return Task.CompletedTask;
101
102 NotificationManager.Notify(Guid.NewGuid().GetHashCode(), Builder.Build());
103 return Task.CompletedTask;
104 }
105
106 private void EnsureChannel(NotificationManager NotificationManagerInstance, string ChannelId)
107 {
108 if (this.createdChannels.Contains(ChannelId) || !OperatingSystem.IsAndroidVersionAtLeast(26))
109 return;
110
111 NotificationChannel Channel = new NotificationChannel(ChannelId, ChannelId, NotificationImportance.High)
112 {
113 LockscreenVisibility = NotificationVisibility.Private
114 };
115 NotificationManagerInstance.CreateNotificationChannel(Channel);
116 this.createdChannels.Add(ChannelId);
117 }
118 }
119}
Platform-neutral intent describing how to route a notification.
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.
string? Channel
Gets or sets the push channel identifier.
Task RenderAsync(NotificationIntent NotificationIntent, CancellationToken CancellationToken)
Displays a local notification with the provided title and message.
Renders local notifications on the platform.
NotificationPresentation
Presentation preference for a notification intent.