Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,27 @@
namespace Plugin.LocalNotifications
{
/// <summary>
/// Local Notifications implementation for UWP, WinRT and Windows Phone Silverlight
/// Local Notifications implementation for UWP
/// </summary>
public class LocalNotificationsImplementation : ILocalNotifications
{
private const string _TOAST_TEXT02_TEMPLATE = "<toast>"
+ "<visual>"
+ "<binding template='ToastText02'>"
+ "<text id='1'>{0}</text>"
+ "<text id='2'>{1}</text>"
+ "</binding>"
+ "</visual>"
+ "</toast>";

public static string ToastTemplate = "<toast>"
+ "<visual>"
+ "<binding template='ToastText02'>"
+ "<text id='1'>{0}</text>"
+ "<text id='2'>{1}</text>"
+ "</binding>"
+ "</visual>"
+ "</toast>";

private readonly ToastNotifier _manager;

public LocalNotificationsImplementation()
{
// Create a toast notifier and show the toast
_manager = ToastNotificationManager.CreateToastNotifier();
}

/// <summary>
/// Show a local notification
Expand All @@ -28,19 +37,15 @@ public class LocalNotificationsImplementation : ILocalNotifications
/// <param name="id">Id of the notification</param>
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);
}

/// <summary>
Expand All @@ -52,7 +57,7 @@ public void Show(string title, string body, int id = 0)
/// <param name="notifyTime">Time to show notification</param>
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);
Expand All @@ -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);
}

/// <summary>
Expand All @@ -75,14 +80,14 @@ public void Show(string title, string body, int id, DateTime notifyTime)
/// <param name="id">Id of the notification to cancel</param>
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);
}
}
}
}
}