public UserNotificationCenterDelegate(string id, IUNUserNotificationCenterDelegate previousDelegate, Action <string, NotificationResult> action, bool cancel, bool allowTapInNotificationCenter) { _action = action; _id = id; _cancel = cancel; _allowTapInNotificationCenter = allowTapInNotificationCenter; _previousDelegate = previousDelegate; }
public INotificationResult Notify(INotificationOptions options) { var notificationCenter = UNUserNotificationCenter.Current; var content = new UNMutableNotificationContent(); content.Title = options.Title; content.Body = options.Description; content.Sound = UNNotificationSound.Default; if (options.iOSOptions != null && options.iOSOptions.SetBadgeCount) { content.Badge = options.iOSOptions.BadgeCount; } UNNotificationTrigger trigger; if (options.DelayUntil.HasValue) { trigger = UNCalendarNotificationTrigger.CreateTrigger(options.DelayUntil.Value.ToNSDateComponents(), false); } else { trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(0.1, false); } var id = _count.ToString(); _count++; var request = UNNotificationRequest.FromIdentifier(id, content, trigger); if (_firstNotification) { _firstNotification = false; PreviousDelegate = notificationCenter.Delegate; } notificationCenter.Delegate = new UserNotificationCenterDelegate(id, PreviousDelegate, (identifier, notificationResult) => { lock (_lock) if (_resetEvents?.ContainsKey(identifier) == true && _eventResult?.ContainsKey(identifier) == false) { _eventResult.Add(identifier, notificationResult); _resetEvents[identifier].Set(); } }, options.ClearFromHistory, options.AllowTapInNotificationCenter); var resetEvent = new ManualResetEvent(false); _resetEvents.Add(id, resetEvent); notificationCenter.AddNotificationRequest(request, (error) => { if (error != null) { _eventResult?.Add(request.Identifier, new NotificationResult() { Action = NotificationAction.Failed }); } }); if (options.DelayUntil.HasValue) { return new NotificationResult() { Action = NotificationAction.NotApplicable } } ; resetEvent.WaitOne(); var result = _eventResult[id]; _resetEvents.Remove(id); _eventResult.Remove(id); return(result); }