3using System.Threading.Tasks;
7using AndroidX.Core.App;
8using AndroidNotification = Android.App.Notification;
10using System.Text.Json;
21 private const string DefaultChannelId =
"default";
22 private readonly HashSet<string> createdChannels =
new HashSet<string>();
33 CancellationToken.ThrowIfCancellationRequested();
37 return Task.CompletedTask;
40 Context? ApplicationContext = Android.App.Application.Context;
41 if (ApplicationContext is
null)
43 return Task.CompletedTask;
45 Context ApplicationContextInstance = ApplicationContext;
49 NotificationManager? NotificationManagerInstance = ApplicationContextInstance.GetSystemService(Context.NotificationService) as NotificationManager;
50 if (NotificationManagerInstance is
null)
52 return Task.CompletedTask;
54 NotificationManager NotificationManager = NotificationManagerInstance;
56 this.EnsureChannel(NotificationManager, ChannelId);
58 int IconId = ApplicationContextInstance.Resources?.GetIdentifier(
61 ApplicationContextInstance.PackageName) ?? ApplicationContextInstance.ApplicationInfo?.Icon ?? Android.Resource.Drawable.SymDefAppIcon;
62 NotificationCompat.Builder? Builder =
new NotificationCompat.Builder(ApplicationContextInstance, ChannelId)
65 .SetSmallIcon(IconId)?
67 .SetPriority((
int)NotificationPriority.High);
69 if (OperatingSystem.IsAndroidVersionAtLeast(26))
71 Builder?.SetCategory(AndroidNotification.CategoryMessage);
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);
82 int RequestCode = Math.Abs(Payload.GetHashCode());
83 PendingIntent? PendingIntentInstance = PendingIntent.GetActivity(
84 ApplicationContextInstance,
87 PendingIntentFlags.UpdateCurrent | PendingIntentFlags.Immutable | PendingIntentFlags.OneShot);
89 if (PendingIntentInstance is not
null)
91 Builder?.SetContentIntent(PendingIntentInstance);
100 return Task.CompletedTask;
102 NotificationManager.Notify(Guid.NewGuid().GetHashCode(), Builder.Build());
103 return Task.CompletedTask;
106 private void EnsureChannel(NotificationManager NotificationManagerInstance,
string ChannelId)
108 if (this.createdChannels.Contains(ChannelId) || !OperatingSystem.IsAndroidVersionAtLeast(26))
111 NotificationChannel Channel =
new NotificationChannel(ChannelId, ChannelId, NotificationImportance.High)
113 LockscreenVisibility = NotificationVisibility.Private
115 NotificationManagerInstance.CreateNotificationChannel(Channel);
116 this.createdChannels.Add(ChannelId);
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.
Renders local notifications on Android.
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.