public async Task <INotificationResult> Notify(INotificationOptions options) { return(await Task.Run(() => { switch (_androidOptions.Style) { case NotificationStyle.Notifications: return _notificationBuilder.Notify(_activity, options); case NotificationStyle.Snackbar: return _snackbarNotification.Notify(_activity, options); default: case NotificationStyle.Default: if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop) { return _notificationBuilder.Notify(_activity, options); } else { return _snackbarNotification.Notify(_activity, options); } } })); }
private void ScheduleNotification(string id, INotificationOptions options) { var intent = new Intent(Application.Context, typeof(AlarmHandler)) .SetAction("AlarmHandlerIntent" + id); var notification = new ScheduledNotification() { AndroidOptions = (AndroidOptions)options.AndroidOptions, ClearFromHistory = options.ClearFromHistory, DelayUntil = options.DelayUntil, Description = options.Description, IsClickable = options.IsClickable, Title = options.Title }; var serializedNotification = Serialize(notification); intent.PutExtra(AlarmHandler.NotificationKey, serializedNotification); intent.PutExtra(NotificationId, id); var pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, intent, PendingIntentFlags.CancelCurrent); var timeTriggered = ConvertToMilliseconds(options.DelayUntil.Value); var alarmManager = Application.Context.GetSystemService(Context.AlarmService) as AlarmManager; alarmManager.Set(AlarmType.RtcWakeup, timeTriggered, pendingIntent); }
void ScheduleNotification(string id, INotificationOptions options) { if (!string.IsNullOrEmpty(id) && options != null) { var intent = new Intent(Application.Context, typeof(AlarmHandler)).SetAction("AlarmHandlerIntent" + id); if (!string.IsNullOrEmpty(options.AndroidOptions.HexColor) && options.AndroidOptions.HexColor.Substring(0, 1) != "#") { options.AndroidOptions.HexColor = "#" + options.AndroidOptions.HexColor; } var notification = new ScheduledNotification() { AndroidOptions = (AndroidOptions)options.AndroidOptions, ClearFromHistory = options.ClearFromHistory, DelayUntil = options.DelayUntil, Description = options.Description, IsClickable = options.IsClickable, Title = options.Title }; var serializedNotification = Serialize(notification); intent.PutExtra(AlarmHandler.NotificationKey, serializedNotification); intent.PutExtra(NotificationId, id); intent.PutExtra(NotificationForceOpenApp, options.AndroidOptions.ForceOpenAppOnNotificationTap); var pendingIntent = PendingIntent.GetBroadcast(Application.Context, (StartId + int.Parse(id)), intent, PendingIntentFlags.CancelCurrent); var timeTriggered = ConvertToMilliseconds(options.DelayUntil.Value); var alarmManager = Application.Context.GetSystemService(Context.AlarmService) as AlarmManager; alarmManager.Set(AlarmType.RtcWakeup, timeTriggered, pendingIntent); } }
public NotificationResult Notify(Activity activity, INotificationOptions options) { var view = activity.FindViewById(Android.Resource.Id.Content); SpannableStringBuilder builder = new SpannableStringBuilder(); builder.Append(options.Title); if (!string.IsNullOrEmpty(options.Title) && !string.IsNullOrEmpty(options.Description)) { builder.Append("\n"); // Max of 2 lines for snackbar } builder.Append(options.Description); var id = _count.ToString(); _count++; var snackbar = Snackbar.Make(view, builder, Snackbar.LengthLong); if (options.IsClickable) { snackbar.SetAction(options.AndroidOptions.ViewText, new EmptyOnClickListener(id, (toastId, result) => { ToastClosed(toastId, result); }, new NotificationResult() { Action = NotificationAction.Clicked })); } else { snackbar.SetAction(options.AndroidOptions.DismissText, new EmptyOnClickListener(id, (toastId, result) => { ToastClosed(toastId, result); }, new NotificationResult() { Action = NotificationAction.Dismissed })); } // Monitor callbacks snackbar.SetCallback(new ToastCallback(id, (toastId, result) => { ToastClosed(toastId, result); })); // Setup reset events var resetEvent = new ManualResetEvent(false); _resetEvents.Add(id, resetEvent); _snackBars.Add(snackbar); snackbar.Show(); resetEvent.WaitOne(); // Wait for a result var notificationResult = _eventResult[id]; _eventResult.Remove(id); _resetEvents.Remove(id); if (_snackBars.Contains(snackbar)) { _snackBars.Remove(snackbar); } return(notificationResult); }
void ShowToast(INotificationOptions options) { var notificator = DependencyService.Get <IToastNotificator>(); notificator.Notify((INotificationResult result) => { System.Diagnostics.Debug.WriteLine("Notification [" + result.Id + "] Result Action: " + result.Action); }, options); }
private async void ShowToast(INotificationOptions options) { var notificator = DependencyService.Get <IToastNotificator>(); if (notificator != null) { var result = await notificator.Notify(options); } }
public void Notify(Action <INotificationResult> callback, INotificationOptions options) { Task.Run(async() => { return(await Notify(options)); }).ContinueWith((task) => { callback.Invoke(task.Result); }); }
async void ShowToast(INotificationOptions options) { await Navigation.PushAsync(new RegisterPage()); //var notificator = DependencyService.Get<IToastNotificator>(); //await notificator.Notify(options); //notificator.Notify((INotificationResult result) => //{ // System.Diagnostics.Debug.WriteLine("Notification [" + result.Id + "] Result Action: " + result.Action); //}, options); }
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; var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(0.1, false); // Trigger now var id = _count.ToString(); _count++; var request = UNNotificationRequest.FromIdentifier(id, content, trigger); notificationCenter.Delegate = new UserNotificationCenterDelegate(id, (identifier, notificationResult) => { lock (_lock) if (_resetEvents.ContainsKey(identifier) && !_eventResult.ContainsKey(identifier)) { _eventResult.Add(identifier, notificationResult); _resetEvents[identifier].Set(); } }); 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 }); } }); resetEvent.WaitOne(); var result = _eventResult[id]; _resetEvents.Remove(id); _eventResult.Remove(id); return(result); }
public void Notify(Action <INotificationResult> callback, INotificationOptions options) { Task.Run(async() => { return(await Notify(options)); }).ContinueWith((task) => { var tResult = task.Result; if (options.AndroidOptions.DebugShowCallbackToast) { _activity.RunOnUiThread(() => { Toast.MakeText(Application.Context, "Notification [" + tResult.Id + "] Result Action: " + tResult.Action, ToastLength.Short).Show(); }); } callback.Invoke(tResult); }); }
private async Task <INotificationResult> Notify(INotificationOptions options) { INotificationResult result = null; return(await Task.Run(() => { if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0)) { return _NotificationManager.Notify(options); } else { ManualResetEvent reset = new ManualResetEvent(false); UIApplication.SharedApplication.InvokeOnMainThread(() => { result = _LocalNotificationManager.Notify(options); reset.Set(); }); reset.WaitOne(); return result; } })); }
public INotificationResult Notify(INotificationOptions options) { // create the notification var notification = new UILocalNotification { // set the fire date (the date time in which it will fire) FireDate = options.DelayUntil == null ? NSDate.Now : options.DelayUntil.Value.ToNSDate(), // configure the alert AlertTitle = options.Title, AlertBody = options.Description, // set the sound to be the default sound SoundName = UILocalNotification.DefaultSoundName }; if (options.CustomArgs != null) { NSMutableDictionary dictionary = new NSMutableDictionary(); foreach (var arg in options.CustomArgs) { dictionary.SetValueForKey(NSObject.FromObject(arg.Value), new NSString(arg.Key)); } // Don't document, this feature is most likely to change dictionary.SetValueForKey(NSObject.FromObject(System.Guid.NewGuid().ToString()), new NSString("Identifier")); notification.UserInfo = dictionary; } // schedule it UIApplication.SharedApplication.ScheduleLocalNotification(notification); return(new NotificationResult() { Action = NotificationAction.NotApplicable }); }
public INotificationResult Notify(Activity activity, INotificationOptions options) { var notificationId = _count; var id = _count.ToString(); _count++; Intent dismissIntent = new Intent(DismissedClickIntent); dismissIntent.PutExtra(NotificationId, notificationId); PendingIntent pendingDismissIntent = PendingIntent.GetBroadcast(activity.ApplicationContext, 123, dismissIntent, 0); Intent clickIntent = new Intent(OnClickIntent); clickIntent.PutExtra(NotificationId, notificationId); // Add custom args if (options.CustomArgs != null) { foreach (var arg in options.CustomArgs) { clickIntent.PutExtra(arg.Key, arg.Value); } } PendingIntent pendingClickIntent = PendingIntent.GetBroadcast(activity.ApplicationContext, 123, clickIntent, 0); int smallIcon; if (options.AndroidOptions.SmallDrawableIcon.HasValue) { smallIcon = options.AndroidOptions.SmallDrawableIcon.Value; } else if (_androidOptions.SmallIconDrawable.HasValue) { smallIcon = _androidOptions.SmallIconDrawable.Value; } else { smallIcon = Android.Resource.Drawable.IcDialogInfo; // As last resort } Android.App.Notification.Builder builder = new Android.App.Notification.Builder(activity) .SetContentTitle(options.Title) .SetContentText(options.Description) .SetSmallIcon(smallIcon) // Must have small icon to display .SetPriority((int)NotificationPriority.High) // Must be set to High to get Heads-up notification .SetDefaults(NotificationDefaults.All) // Must also include vibrate to get Heads-up notification .SetAutoCancel(true) // To allow click event to trigger delete Intent .SetContentIntent(pendingClickIntent) // Must have Intent to accept the click .SetDeleteIntent(pendingDismissIntent); Android.App.Notification notification = builder.Build(); NotificationManager notificationManager = activity.GetSystemService(Context.NotificationService) as NotificationManager; notificationManager.Notify(notificationId, notification); var timer = new Timer(x => TimerFinished(id), null, TimeSpan.FromSeconds(7), TimeSpan.FromSeconds(100)); var resetEvent = new ManualResetEvent(false); ResetEvent.Add(id, resetEvent); resetEvent.WaitOne(); // Wait for a result var notificationResult = EventResult[id]; if (!options.IsClickable && notificationResult.Action == NotificationAction.Clicked) { notificationResult.Action = NotificationAction.Dismissed; } EventResult.Remove(id); ResetEvent.Remove(id); // Dispose of Intents and Timer pendingClickIntent.Cancel(); pendingDismissIntent.Cancel(); timer.Dispose(); return(notificationResult); }
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); }
public INotificationResult Notify(INotificationOptions options) { var notificationCenter = UNUserNotificationCenter.Current; var content = new UNMutableNotificationContent { Title = options.Title, Body = options.Description, Sound = UNNotificationSound.Default }; 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); notificationCenter.Delegate = new UserNotificationCenterDelegate(id, (identifier, notificationResult) => { lock (Lock) { if (_ResetEvents?.ContainsKey(identifier) == true && _EventResult?.ContainsKey(identifier) == false) { _EventResult.Add(identifier, notificationResult); _ResetEvents[identifier].Set(); } } }, options.ClearFromHistory); 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); }
public void Notify(Action <INotificationResult> callback, INotificationOptions options) { Task.Run(async() => callback(await Notify(options)) ); }
public Task <INotificationResult> Notify(INotificationOptions options) { return(Task.Run(() => { ToastNotifier ToastNotifier = ToastNotificationManager.CreateToastNotifier(); Windows.Data.Xml.Dom.XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02); Windows.Data.Xml.Dom.XmlNodeList toastNodeList = toastXml.GetElementsByTagName("text"); toastNodeList.Item(0).AppendChild(toastXml.CreateTextNode(options.Title)); toastNodeList.Item(1).AppendChild(toastXml.CreateTextNode(options.Description)); Windows.Data.Xml.Dom.IXmlNode toastNode = toastXml.SelectSingleNode("/toast"); var id = _count.ToString(); var root = toastXml.DocumentElement; root.SetAttribute("launch", id.ToString()); if (!string.IsNullOrEmpty(options.WindowsOptions.LogoUri)) { Windows.Data.Xml.Dom.XmlElement image = toastXml.CreateElement("image"); image.SetAttribute("placement", "appLogoOverride"); var imageUri = options.WindowsOptions.LogoUri; if (!options.WindowsOptions.LogoUri.Contains("//")) { imageUri = $"ms-appx:///{options.WindowsOptions.LogoUri}"; } image.SetAttribute("src", imageUri); toastXml.GetElementsByTagName("binding")[0].AppendChild(image); toastXml.GetElementsByTagName("binding")[0].Attributes[0].InnerText = "ToastGeneric"; } if (options.DelayUntil.HasValue) { ScheduledToastNotification toast = new ScheduledToastNotification(toastXml, options.DelayUntil.Value); ToastNotifier.AddToSchedule(toast); return new NotificationResult() { Action = NotificationAction.NotApplicable }; } else { Windows.UI.Notifications.ToastNotification toast = new Windows.UI.Notifications.ToastNotification(toastXml); toast.Tag = id; _count++; toast.Dismissed += Toast_Dismissed; toast.Activated += Toast_Activated; toast.Failed += Toast_Failed; _notificationOptions.Add(id, options); var waitEvent = new ManualResetEvent(false); _resetEvents.Add(id, waitEvent); ToastNotifier.Show(toast); waitEvent.WaitOne(); INotificationResult result = _eventResult[id]; if (!options.IsClickable && result.Action == NotificationAction.Clicked) // A click is transformed to manual dismiss { result = new NotificationResult() { Action = NotificationAction.Dismissed } } ; if (_resetEvents.ContainsKey(id)) { _resetEvents.Remove(id); } if (_eventResult.ContainsKey(id)) { _eventResult.Remove(id); } if (_notificationOptions.ContainsKey(id)) { _notificationOptions.Remove(id); } return result; } })); }
public INotificationResult Notify(Activity activity, INotificationOptions options) { INotificationResult notificationResult = null; if (options != null) { var notificationId = 0; var id = ""; lock (_lock) { notificationId = _count; id = _count.ToString(); _count++; } int smallIcon; if (options.AndroidOptions.SmallDrawableIcon.HasValue) { smallIcon = options.AndroidOptions.SmallDrawableIcon.Value; } else if (_androidOptions.SmallIconDrawable.HasValue) { smallIcon = _androidOptions.SmallIconDrawable.Value; } else { smallIcon = Android.Resource.Drawable.IcDialogInfo; // As last resort } if (options.DelayUntil.HasValue) { options.AndroidOptions.SmallDrawableIcon = smallIcon; ScheduleNotification(id, options); return(new NotificationResult() { Action = NotificationAction.NotApplicable, Id = notificationId }); } // Show Notification Right Now var dismissIntent = new Intent(DismissedClickIntent); dismissIntent.PutExtra(NotificationId, notificationId); var pendingDismissIntent = PendingIntent.GetBroadcast(Application.Context, (StartId + notificationId), dismissIntent, 0); var clickIntent = new Intent(OnClickIntent); clickIntent.PutExtra(NotificationId, notificationId); clickIntent.PutExtra(NotificationForceOpenApp, options.AndroidOptions.ForceOpenAppOnNotificationTap); // Add custom args if (options.CustomArgs != null) { foreach (var arg in options.CustomArgs) { clickIntent.PutExtra(arg.Key, arg.Value); } } var pendingClickIntent = PendingIntent.GetBroadcast(Application.Context, (StartId + notificationId), clickIntent, 0); if (!string.IsNullOrEmpty(options.AndroidOptions.HexColor) && options.AndroidOptions.HexColor.Substring(0, 1) != "#") { options.AndroidOptions.HexColor = "#" + options.AndroidOptions.HexColor; } Android.App.Notification.Builder builder = new Android.App.Notification.Builder(Application.Context) .SetContentTitle(options.Title) .SetContentText(options.Description) .SetSmallIcon(smallIcon) // Must have small icon to display .SetPriority((int)NotificationPriority.High) // Must be set to High to get Heads-up notification .SetDefaults(NotificationDefaults.All) // Must also include vibrate to get Heads-up notification .SetAutoCancel(true) // To allow click event to trigger delete Intent .SetContentIntent(pendingClickIntent) // Must have Intent to accept the click .SetDeleteIntent(pendingDismissIntent) .SetColor(Color.ParseColor(options.AndroidOptions.HexColor)); try { // Notification Channel if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O) { var notificationChannelId = GetOrCreateChannel(options.AndroidOptions.ChannelOptions); if (!string.IsNullOrEmpty(notificationChannelId)) { builder.SetChannelId(notificationChannelId); } } } catch { } // System.MissingMethodException: Method 'Android.App.Notification/Builder.SetChannelId' not found. // I know this is bad, but I can't replicate it on any version, and many people are experiencing it. Android.App.Notification notification = builder.Build(); NotificationManager notificationManager = Application.Context.GetSystemService(Context.NotificationService) as NotificationManager; notificationManager.Notify(notificationId, notification); if (options.DelayUntil.HasValue) { return(new NotificationResult() { Action = NotificationAction.NotApplicable, Id = notificationId }); } var timer = new Timer(x => TimerFinished(id, options.ClearFromHistory, options.AllowTapInNotificationCenter), null, TimeSpan.FromSeconds(7), TimeSpan.FromMilliseconds(-1)); var resetEvent = new ManualResetEvent(false); ResetEvent.Add(id, resetEvent); resetEvent.WaitOne(); // Wait for a result notificationResult = EventResult[id]; if (!options.IsClickable && notificationResult.Action == NotificationAction.Clicked) { notificationResult.Action = NotificationAction.Dismissed; notificationResult.Id = notificationId; } if (EventResult.ContainsKey(id)) { EventResult.Remove(id); } if (ResetEvent.ContainsKey(id)) { ResetEvent.Remove(id); } // Dispose of Intents and Timer pendingClickIntent.Cancel(); pendingDismissIntent.Cancel(); timer.Dispose(); } return(notificationResult); }