5using Firebase.CloudMessaging;
10using UserNotifications;
17 [Export(
"userNotificationCenter:willPresentNotification:withCompletionHandler:")]
18 public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
20 var userInfo = notification.Request.Content.UserInfo;
22 Messaging.SharedInstance.AppDidReceiveMessage(userInfo);
24 Console.WriteLine(
"Foreground notification received: " + userInfo);
29 if (intent is not
null)
32 string raw = JsonSerializer.Serialize(userInfo);
42 completionHandler(UNNotificationPresentationOptions.None);
46 [Export(
"userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
47 public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
49 var userInfo = response.Notification.Request.Content.UserInfo;
50 Console.WriteLine(
"Notification tapped with data: " + userInfo);
55 if (intent is not
null)
58 string raw = JsonSerializer.Serialize(userInfo);
61 service.
ConsumeAsync(
id, CancellationToken.None).ConfigureAwait(
false);
72 public static void ProcessSilentNotification(NSDictionary userInfo)
80 string raw = JsonSerializer.Serialize(userInfo);
91 private NotificationIntent? ParseIntent(NSDictionary userInfo) => ParseIntentStatic(userInfo);
97 if (userInfo[
"notificationIntent"] is NSString intentJson)
100 if (parsedNotification is not
null)
101 return parsedNotification;
104 string? channelId = userInfo[
"channelId"]?.ToString();
105 string? title = userInfo[
"myTitle"]?.ToString();
106 string? body = userInfo[
"myBody"]?.ToString();
108 if (channelId is
null)
114 Title = title ??
string.Empty,
118 foreach (NSObject key
in userInfo.Keys)
120 string k = key.ToString();
121 string v = userInfo[key]?.ToString() ??
string.Empty;
125 if (intent.Extras.TryGetValue(
"action", out
string? action) && Enum.TryParse(action, out
NotificationAction parsed))
126 intent.Action = parsed;
128 if (intent.Extras.TryGetValue(
"entityId", out
string? entityId))
129 intent.EntityId = entityId;
131 if (intent.Extras.TryGetValue(
"correlationId", out
string? correlationId))
132 intent.CorrelationId = correlationId;
134 intent.Presentation = ResolvePresentation(intent.Extras);
146 if (extras.TryGetValue(
"silent", out
string silent) && IsTrue(silent))
149 if (extras.TryGetValue(
"delivery.silent", out
string deliverySilent) && IsTrue(deliverySilent))
155 private static bool IsTrue(
string value)
157 return value.Equals(
"1", StringComparison.OrdinalIgnoreCase) ||
158 value.Equals(
"true", StringComparison.OrdinalIgnoreCase) ||
159 value.Equals(
"yes", StringComparison.OrdinalIgnoreCase);
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.
static IServiceProvider Provider
The service provider for the app. This is set before the app is started, and will be used to resolve ...
static ILogService LogService
Log service.
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.