/// <inheritdoc /> public async void Show(NotificationRequest notificationRequest) { UNNotificationTrigger trigger = null; try { if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0) == false) { return; } if (notificationRequest is null) { return; } var allowed = await NotificationCenter.AskPermissionAsync().ConfigureAwait(false); if (allowed == false) { return; } var userInfoDictionary = new NSMutableDictionary(); if (string.IsNullOrWhiteSpace(notificationRequest.ReturningData) == false) { using var returningData = new NSString(notificationRequest.ReturningData); userInfoDictionary.SetValueForKey( string.IsNullOrWhiteSpace(notificationRequest.ReturningData) ? NSString.Empty : returningData, NotificationCenter.ExtraReturnDataIos); } using var receivedData = new NSString(notificationRequest.iOS.HideForegroundAlert.ToString()); userInfoDictionary.SetValueForKey(receivedData, NotificationCenter.ExtraNotificationReceivedIos); using var soundData = new NSString(notificationRequest.iOS.PlayForegroundSound.ToString()); userInfoDictionary.SetValueForKey(soundData, NotificationCenter.ExtraSoundInForegroundIos); using var content = new UNMutableNotificationContent { Title = notificationRequest.Title, Body = notificationRequest.Description, Badge = notificationRequest.BadgeNumber, UserInfo = userInfoDictionary, Sound = UNNotificationSound.Default }; if (string.IsNullOrWhiteSpace(notificationRequest.Sound) == false) { content.Sound = UNNotificationSound.GetSound(notificationRequest.Sound); } var repeats = notificationRequest.Repeats != NotificationRepeat.No; if (repeats && notificationRequest.Repeats == NotificationRepeat.TimeInterval && notificationRequest.NotifyRepeatInterval.HasValue) { TimeSpan interval = notificationRequest.NotifyRepeatInterval.Value; trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(interval.TotalSeconds, true); } else { using var notifyTime = GetNsDateComponentsFromDateTime(notificationRequest); trigger = UNCalendarNotificationTrigger.CreateTrigger(notifyTime, repeats); } var notificationId = notificationRequest.NotificationId.ToString(CultureInfo.CurrentCulture); var request = UNNotificationRequest.FromIdentifier(notificationId, content, trigger); await UNUserNotificationCenter.Current.AddNotificationRequestAsync(request) .ConfigureAwait(false); } catch (Exception ex) { Debug.WriteLine(ex); } finally { trigger?.Dispose(); } }
/// <inheritdoc /> public async Task <bool> Show(NotificationRequest request) { UNNotificationTrigger trigger = null; try { if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0) == false) { return(false); } if (request is null) { return(false); } var allowed = await NotificationCenter.AskPermissionAsync().ConfigureAwait(false); if (allowed == false) { return(false); } var userInfoDictionary = new NSMutableDictionary(); var dictionary = NotificationCenter.GetRequestSerialize(request); foreach (var item in dictionary) { userInfoDictionary.SetValueForKey(new NSString(item.Value), new NSString(item.Key)); } using (var content = new UNMutableNotificationContent { Title = request.Title, Subtitle = request.Subtitle, Body = request.Description, Badge = request.BadgeNumber, UserInfo = userInfoDictionary, Sound = UNNotificationSound.Default, }) { // Image Attachment if (request.Image != null) { var nativeImage = await GetNativeImage(request.Image); if (nativeImage != null) { content.Attachments = new[] { nativeImage }; } } if (request.CategoryType != NotificationCategoryType.None) { content.CategoryIdentifier = ToNativeCategory(request.CategoryType); } if (string.IsNullOrWhiteSpace(request.Sound) == false) { content.Sound = UNNotificationSound.GetSound(request.Sound); } var repeats = request.Schedule.RepeatType != NotificationRepeat.No; if (repeats && request.Schedule.RepeatType == NotificationRepeat.TimeInterval && request.Schedule.NotifyRepeatInterval.HasValue) { TimeSpan interval = request.Schedule.NotifyRepeatInterval.Value; // Cannot delay and repeat in when TimeInterval trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(interval.TotalSeconds, true); } else { using (var notifyTime = GetNsDateComponentsFromDateTime(request)) { trigger = UNCalendarNotificationTrigger.CreateTrigger(notifyTime, repeats); } } var notificationId = request.NotificationId.ToString(CultureInfo.CurrentCulture); var nativeRequest = UNNotificationRequest.FromIdentifier(notificationId, content, trigger); await UNUserNotificationCenter.Current.AddNotificationRequestAsync(nativeRequest) .ConfigureAwait(false); return(true); } } catch (Exception ex) { Debug.WriteLine(ex); return(false); } finally { trigger?.Dispose(); } }