From fffb8989eb869f618abd6289c72b4375bb91fe16 Mon Sep 17 00:00:00 2001 From: Paul Datsiuk Date: Thu, 9 Aug 2018 14:46:13 +0300 Subject: [PATCH] uwp - repaired logic of scheduled notifications --- .../LocalNotificationsImplementation.cs | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/src/Plugin.LocalNotifications.UWP/LocalNotificationsImplementation.cs b/src/Plugin.LocalNotifications.UWP/LocalNotificationsImplementation.cs index 8fe32d0..6f68e5a 100644 --- a/src/Plugin.LocalNotifications.UWP/LocalNotificationsImplementation.cs +++ b/src/Plugin.LocalNotifications.UWP/LocalNotificationsImplementation.cs @@ -7,18 +7,27 @@ namespace Plugin.LocalNotifications { /// - /// Local Notifications implementation for UWP, WinRT and Windows Phone Silverlight + /// Local Notifications implementation for UWP /// public class LocalNotificationsImplementation : ILocalNotifications { - private const string _TOAST_TEXT02_TEMPLATE = "" - + "" - + "" - + "{0}" - + "{1}" - + "" - + "" - + ""; + + public static string ToastTemplate = "" + + "" + + "" + + "{0}" + + "{1}" + + "" + + "" + + ""; + + private readonly ToastNotifier _manager; + + public LocalNotificationsImplementation() + { + // Create a toast notifier and show the toast + _manager = ToastNotificationManager.CreateToastNotifier(); + } /// /// Show a local notification @@ -28,19 +37,15 @@ public class LocalNotificationsImplementation : ILocalNotifications /// Id of the notification public void Show(string title, string body, int id = 0) { - var xmlData = string.Format(_TOAST_TEXT02_TEMPLATE, title, body); + var xmlData = string.Format(ToastTemplate, title, body); var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlData); // Create a toast var toast = new ToastNotification(xmlDoc); - - // Create a toast notifier and show the toast - var manager = ToastNotificationManager.CreateToastNotifier(); - - manager.Show(toast); + _manager.Show(toast); } /// @@ -52,7 +57,7 @@ public void Show(string title, string body, int id = 0) /// Time to show notification public void Show(string title, string body, int id, DateTime notifyTime) { - var xmlData = string.Format(_TOAST_TEXT02_TEMPLATE, title, body); + var xmlData = string.Format(ToastTemplate, title, body); var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xmlData); @@ -61,12 +66,12 @@ public void Show(string title, string body, int id, DateTime notifyTime) ? DateTime.Now.AddMilliseconds(100) : notifyTime; - var scheduledTileNotification = new ScheduledTileNotification(xmlDoc, correctedTime) + var scheduledTileNotification = new ScheduledToastNotification(xmlDoc, correctedTime) { Id = id.ToString() }; - TileUpdateManager.CreateTileUpdaterForApplication().AddToSchedule(scheduledTileNotification); + _manager.AddToSchedule(scheduledTileNotification); } /// @@ -75,14 +80,14 @@ public void Show(string title, string body, int id, DateTime notifyTime) /// Id of the notification to cancel public void Cancel(int id) { - var scheduledNotifications = TileUpdateManager.CreateTileUpdaterForApplication().GetScheduledTileNotifications(); + var scheduledNotifications = _manager.GetScheduledToastNotifications(); var notification = scheduledNotifications.FirstOrDefault(n => n.Id.Equals(id.ToString(), StringComparison.OrdinalIgnoreCase)); if (notification != null) { - TileUpdateManager.CreateTileUpdaterForApplication().RemoveFromSchedule(notification); + _manager.RemoveFromSchedule(notification); } } } -} +} \ No newline at end of file