internal void OnBuildNotification(NotificationCompat.Builder notificationBuilder, NotificationDto notification) { if (!string.IsNullOrWhiteSpace(notification.Subject)) { notificationBuilder.SetContentTitle(notification.Subject); } if (!string.IsNullOrWhiteSpace(notification.ImageSmall)) { int width = GetDimension(Resource.Dimension.notification_large_icon_width); int height = GetDimension(Resource.Dimension.notification_large_icon_height); var largeIcon = GetBitmap(notification.ImageSmall, width, height); if (largeIcon != null) { notificationBuilder.SetLargeIcon(largeIcon); } } if (!string.IsNullOrWhiteSpace(notification.ImageLarge)) { var bigPicture = GetBitmap(notification.ImageLarge); if (bigPicture != null) { notificationBuilder.SetStyle( new NotificationCompat .BigPictureStyle() .BigPicture(bigPicture) .SetSummaryText(notification.Body) ); } } notificationBuilder.MActions.Clear(); if (!string.IsNullOrWhiteSpace(notification.ConfirmUrl) && !string.IsNullOrWhiteSpace(notification.ConfirmText) && !notification.IsConfirmed) { AddAction(notificationBuilder, notification.ConfirmText, notification.ConfirmUrl); } if (!string.IsNullOrWhiteSpace(notification.LinkUrl) && !string.IsNullOrWhiteSpace(notification.LinkText)) { AddAction(notificationBuilder, notification.LinkText, notification.LinkUrl); } notificationHandler?.OnBuildNotification(notificationBuilder, notification); }
private async Task <UNMutableNotificationContent> EnrichNotificationContentAsync(UNMutableNotificationContent content, NotificationDto notification) { if (!string.IsNullOrWhiteSpace(notification.Subject)) { content.Title = notification.Subject; } if (!string.IsNullOrWhiteSpace(notification.Body)) { content.Body = notification.Body; } string image = string.IsNullOrWhiteSpace(notification.ImageLarge) ? notification.ImageSmall : notification.ImageLarge; if (!string.IsNullOrWhiteSpace(image)) { var imagePath = await GetImageAsync(image); if (!string.IsNullOrWhiteSpace(imagePath)) { var uniqueName = $"{Guid.NewGuid()}{Path.GetExtension(imagePath)}"; var attachementUrl = new NSUrl(uniqueName, NSFileManager.DefaultManager.GetTemporaryDirectory()); NSFileManager.DefaultManager.Copy(NSUrl.FromFilename(imagePath), attachementUrl, out var error); if (error != null) { Log.Error(error.LocalizedDescription); } var attachement = UNNotificationAttachment.FromIdentifier( Constants.ImageLargeKey, attachementUrl, new UNNotificationAttachmentOptions(), out error); if (error == null) { content.Attachments = new UNNotificationAttachment[] { attachement }; } else { Log.Error(error.LocalizedDescription); } } } var actions = new List <UNNotificationAction>(); if (!string.IsNullOrWhiteSpace(notification.ConfirmUrl) && !string.IsNullOrWhiteSpace(notification.ConfirmText) && !notification.IsConfirmed) { var confirmAction = UNNotificationAction.FromIdentifier( Constants.ConfirmAction, notification.ConfirmText, UNNotificationActionOptions.Foreground); actions.Add(confirmAction); } if (!string.IsNullOrWhiteSpace(notification.LinkUrl) && !string.IsNullOrWhiteSpace(notification.LinkText)) { var linkAction = UNNotificationAction.FromIdentifier( Constants.LinkAction, notification.LinkText, UNNotificationActionOptions.Foreground); actions.Add(linkAction); } if (actions.Any()) { var categoryId = Guid.NewGuid().ToString(); var newCategory = UNNotificationCategory.FromIdentifier( categoryId, actions.ToArray(), new string[] { }, UNNotificationCategoryOptions.None); var categories = new List <UNNotificationCategory>(); var allCategories = await UNUserNotificationCenter.Current.GetNotificationCategoriesAsync(); if (allCategories != null) { foreach (UNNotificationCategory category in allCategories) { if (category.Identifier != categoryId) { categories.Add(category); } } categories.Add(newCategory); } else { categories.Add(newCategory); } UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet <UNNotificationCategory>(categories.ToArray())); // without this call action buttons won't be added or updated _ = await UNUserNotificationCenter.Current.GetNotificationCategoriesAsync(); content.CategoryIdentifier = categoryId; } if (content.Sound == null) { content.Sound = UNNotificationSound.Default; } notificationHandler?.OnBuildNotification(content, notification); return(content); }