public void ScheduleNotification(UNNotificationContent content, DateTime notifyTime, string requestId) { Debug.WriteLine("DHB:Notifications:ScheduleNotification begin"); Foundation.NSDateComponents notificationContentNSCDate = new Foundation.NSDateComponents(); notificationContentNSCDate.Year = notifyTime.Year; notificationContentNSCDate.Month = notifyTime.Month; notificationContentNSCDate.Day = notifyTime.Day; notificationContentNSCDate.Hour = notifyTime.Hour; notificationContentNSCDate.Minute = notifyTime.Minute; notificationContentNSCDate.Second = notifyTime.Second; //notificationContentNSCDate.TimeZone = Debug.WriteLine("DHB:Notifications:ScheduleNotification sched time:" + notificationContentNSCDate.ToString()); Debug.WriteLine("DHB:Notifications:ScheduleNotification now time:" + notifyTime.ToString()); // repeats makes no sense as just a bool. what does that mean? everyday, every 5 mins, wat wat? UNNotificationTrigger trigger = UNCalendarNotificationTrigger.CreateTrigger(notificationContentNSCDate, true); //string requestId = "firstRequest"; var request = UNNotificationRequest.FromIdentifier(requestId, content, trigger); UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => { if (err != null) { Debug.WriteLine("DHB:iOS:Notifications:ScheduleNotification err:" + err.ToString()); } }); Debug.WriteLine("DHB:Notifications:ScheduleNotification scheduled"); }
static void ShowUserNotification(string title, string body, int id, UNNotificationTrigger trigger, bool playSound = false, Dictionary <string, string> parameters = null) { if (OS.IsBeforeiOS(10)) { return; } var userData = NSDictionary.FromObjectsAndKeys( new NSObject[] { id.ToString().ToNs(), parameters.DicToString().ToNs(), title.ToNs(), body.ToNs(), NSDate.Now }, new NSObject[] { NOTIFICATION_KEY.ToNs(), NOTIFICATION_PARAM_KEY.ToNs(), NOTIFICATION_TITLE_KEY.ToNs(), NOTIFICATION_BODY_KEY.ToNs(), NOTIFICATION_Date_KEY.ToNs() }); var content = new UNMutableNotificationContent() { Title = title, Body = body, UserInfo = userData }; content.Sound = UNNotificationSound.Default; var request = UNNotificationRequest.FromIdentifier(id.ToString(), content, trigger); UNUserNotificationCenter.Current.AddNotificationRequest(request, (error) => { }); }
// Show local notifications using the UNUserNotificationCenter using a notification trigger (iOS 10+ only) static void ShowUserNotification(string title, string body, int id, UNNotificationTrigger trigger) { if (!UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) { return; } var content = new UNMutableNotificationContent() { Title = title, Body = body, Badge = 1, }; var request = UNNotificationRequest.FromIdentifier(id.ToString(), content, trigger); UNUserNotificationCenter.Current.AddNotificationRequest(request, (error) => { if (error != null) { Crashes.TrackError(new Exception(error.ToString())); #if DEBUG Device.BeginInvokeOnMainThread(() => throw new Exception("Notification error. " + error)); #endif } }); }
private void ShowUserNotification(UNNotificationTrigger trigger) { if (!UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) { return; } var content = new UNMutableNotificationContent { UserInfo = GetUserInfo(), }; if (_title != null) { content.Title = _title; } if (_body != null) { content.Body = _body; } if (_actionSetId != null) { content.CategoryIdentifier = _actionSetId; } var request = UNNotificationRequest.FromIdentifier(_id.ToString(), content, trigger); UNUserNotificationCenter.Current.AddNotificationRequest(request, error => { }); }
public async Task Send(Notification notification) { if (notification.Id == 0) { notification.Id = this.settings.IncrementValue("NotificationId"); } var access = await this.RequestAccess(); access.Assert(); var content = new UNMutableNotificationContent { Title = notification.Title, Body = notification.Message, Badge = notification.BadgeCount //LaunchImageName = "" //Subtitle = "" }; //UNNotificationAttachment.FromIdentifier("", NSUrl.FromString(""), new UNNotificationAttachmentOptions().) if (!notification.Payload.IsEmpty()) { var dict = new NSMutableDictionary(); dict.Add(new NSString("Payload"), new NSString(notification.Payload)); content.UserInfo = dict; } if (!Notification.CustomSoundFilePath.IsEmpty()) { content.Sound = UNNotificationSound.GetSound(Notification.CustomSoundFilePath); } UNNotificationTrigger trigger = null; if (notification.ScheduleDate != null) { var dt = notification.ScheduleDate.Value.ToLocalTime(); trigger = UNCalendarNotificationTrigger.CreateTrigger(new NSDateComponents { Year = dt.Year, Month = dt.Month, Day = dt.Day, Hour = dt.Hour, Minute = dt.Minute, Second = dt.Second }, false); } var request = UNNotificationRequest.FromIdentifier( notification.Id.ToString(), content, trigger ); await UNUserNotificationCenter .Current .AddNotificationRequestAsync(request); }
/// <summary> /// Code base logic of showing a notification /// </summary> /// <param name="content">the content of the notification</param> /// <param name="trigger">the conditions that trigger the notification</param> private void BaseNotify(int id, UNMutableNotificationContent content, UNNotificationTrigger trigger) { var request = UNNotificationRequest.FromIdentifier(CreateRequestIdForm(id), content, trigger); UNUserNotificationCenter.Current.AddNotificationRequest(request, err => { if (err != null) { Debug.WriteLine("Adding Notification Failed " + err.DebugDescription); Debug.WriteLine("Adding Notification Failed " + err.LocalizedFailureReason); } }); var requests = UNUserNotificationCenter.Current.GetPendingNotificationRequestsAsync().Result; }
// Show local notifications using the UNUserNotificationCenter using a notification trigger (iOS 10+ only) void ShowUserNotification(string title, string body, string id, UNNotificationTrigger trigger) { if (!UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) { return; } var content = new UNMutableNotificationContent { Title = title, Body = body }; var request = UNNotificationRequest.FromIdentifier(id, content, trigger); UNUserNotificationCenter.Current.AddNotificationRequest(request, (error) => { }); }
/// <summary> /// Show a simple timed text notification, using native iOS Notification APIs /// </summary> /// <param name="text">Text to show in notification</param> /// <param name="time">Time to wait before sending notification</param> public void SetTimedNotification(string text, TimeSpan time) { UNMutableNotificationContent content = new UNMutableNotificationContent() { Title = "SwinApp", Body = text, Badge = 1 }; UNNotificationTrigger trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(Math.Round(time.TotalSeconds), false); string requestID = "SwinApp-notification"; UNNotificationRequest request = UNNotificationRequest.FromIdentifier(requestID, content, trigger); UNUserNotificationCenter.Current.AddNotificationRequest(request, null); }
/// <summary> /// Show a simple text notification in 5 seconds, using native iOS Notification APIs /// </summary> /// <param name="text">Text to show in notification</param> public void ShowTextNotification(string text) { UNMutableNotificationContent content = new UNMutableNotificationContent() { Title = "SwinApp", Body = text, Badge = 1 }; UNNotificationTrigger trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(5D, false); string requestID = "SwinApp-notification"; UNNotificationRequest request = UNNotificationRequest.FromIdentifier(requestID, content, trigger); UNUserNotificationCenter.Current.AddNotificationRequest(request, null); }
void ShowUserNotification(string title, string body, int id, UNNotificationTrigger trigger) { if (!UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) { return; } var content = new UNMutableNotificationContent() { Title = title, Body = body, UserInfo = NSDictionary.FromObjectAndKey(NSObject.FromObject(id), NSObject.FromObject(NotificationKey)) }; var request = UNNotificationRequest.FromIdentifier(id.ToString(), content, trigger); UNUserNotificationCenter.Current.AddNotificationRequest(request, (error) => { }); }
private static void ShowUserNotification(string title, string body, int id, UNNotificationTrigger trigger) { if (!UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) { return; } var content = new UNMutableNotificationContent() { Title = title, Body = body }; var request = UNNotificationRequest.FromIdentifier(id.ToString(), content, trigger); UNUserNotificationCenter.Current.AddNotificationRequest(request, error => CrossVibrate.Current.Vibration(TimeSpan.FromMilliseconds(150))); }
void ShowUserNotification(string title, string body, int id, int badge, UNNotificationTrigger trigger) { if (badge == 0) { UpdateBadge(0); } else { var content = new UNMutableNotificationContent() { Title = title, Body = body, Badge = new NSNumber(badge), Sound = UNNotificationSound.Default }; var request = UNNotificationRequest.FromIdentifier(id.ToString(), content, trigger); UNUserNotificationCenter.Current.AddNotificationRequest(request, (error) => System.Diagnostics.Debug.WriteLine("Error: {0}", error)); } }
/// <summary> /// Show local notifications using the UNUserNotificationCenter using a notification trigger (iOS 10+ only) /// </summary> /// <param name="title">Title of the notification</param> /// <param name="body">Body or description of the notification</param> /// <param name="id">Id of the notificatio</param> /// <param name="trigger">Trigger firing notification</param> /// <param name="customData">Custom data attached to notification</param> private void ShowUserNotification(string title, string body, int id, UNNotificationTrigger trigger, string customData) { if (!UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) { return; } NSDictionary userInfo; if (!string.IsNullOrWhiteSpace(customData)) { userInfo = NSDictionary.FromObjectsAndKeys( new NSObject[] { NSObject.FromObject(id), NSObject.FromObject(customData), }, new NSObject[] { NSObject.FromObject(NotificationKey), NSObject.FromObject(CrossLocalNotifications.LocalNotificationCustomData), }); } else { userInfo = NSDictionary.FromObjectAndKey(NSObject.FromObject(id), NSObject.FromObject(NotificationKey)); } var content = new UNMutableNotificationContent() { Title = title, Body = body, UserInfo = userInfo, Sound = UNNotificationSound.Default }; var request = UNNotificationRequest.FromIdentifier(id.ToString(), content, trigger); UNUserNotificationCenter.Current.AddNotificationRequest(request, (error) => { }); }
/// <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(); } }