|
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Windows.Forms; |
| 5 | +using System.Xml.Linq; |
| 6 | +using Windows.UI.Notifications; |
| 7 | +using WireSockUI.Properties; |
| 8 | + |
| 9 | +namespace WireSockUI.Native |
| 10 | +{ |
| 11 | + internal static class Notifications |
| 12 | + { |
| 13 | + private static string icon; |
| 14 | + private static Windows.Data.Xml.Dom.XmlDocument GetXml(string title, string body) |
| 15 | + { |
| 16 | + XElement toast = |
| 17 | + new XElement("toast", |
| 18 | + new XElement("visual", |
| 19 | + new XElement("binding", |
| 20 | + new XAttribute("template", "ToastGeneric"), |
| 21 | + new XElement("text", title), |
| 22 | + new XElement("text", body), |
| 23 | + new XElement("image", |
| 24 | + new XAttribute("src", icon), |
| 25 | + new XAttribute("placement", "appLogoOverride"), |
| 26 | + new XAttribute("hint-crop", "circle"))))); |
| 27 | + |
| 28 | + Windows.Data.Xml.Dom.XmlDocument xml = new Windows.Data.Xml.Dom.XmlDocument(); |
| 29 | + xml.LoadXml(toast.ToString()); |
| 30 | + |
| 31 | + return xml; |
| 32 | + } |
| 33 | + |
| 34 | + private static void ToastNotificationOnDismissed(ToastNotification sender, ToastDismissedEventArgs args) |
| 35 | + { |
| 36 | + switch (args.Reason) |
| 37 | + { |
| 38 | + case ToastDismissalReason.ApplicationHidden: |
| 39 | + break; |
| 40 | + case ToastDismissalReason.TimedOut: |
| 41 | + break; |
| 42 | + case ToastDismissalReason.UserCanceled: |
| 43 | + break; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + static Notifications() |
| 48 | + { |
| 49 | + // Write the icon to local appdata folder for toast notifications |
| 50 | + icon = $@"{Global.MainFolder}\WireSock.ico"; |
| 51 | + |
| 52 | + if (!File.Exists(icon)) |
| 53 | + using (FileStream stream = new FileStream(icon, FileMode.CreateNew)) |
| 54 | + Resources.ico.Save(stream); |
| 55 | + } |
| 56 | + |
| 57 | + public static void Notify(string title, string body) |
| 58 | + { |
| 59 | + WindowsApplicationContext context = WindowsApplicationContext.FromCurrentProcess(); |
| 60 | + ToastNotifier notifier = ToastNotificationManager.CreateToastNotifier(context.AppUserModelId); |
| 61 | + |
| 62 | + ToastNotification notification = new ToastNotification(GetXml(title, body)); |
| 63 | + |
| 64 | + notification.Activated += Notification_Activated; |
| 65 | + notification.Dismissed += Notification_Dismissed; |
| 66 | + notification.Failed += Notification_Failed; |
| 67 | + |
| 68 | + notifier.Show(notification); |
| 69 | + } |
| 70 | + |
| 71 | + private static void Notification_Failed(ToastNotification sender, ToastFailedEventArgs args) |
| 72 | + { |
| 73 | + Debug.WriteLine($"Notification failed: {args.ErrorCode}"); |
| 74 | + } |
| 75 | + |
| 76 | + private static void Notification_Dismissed(ToastNotification sender, ToastDismissedEventArgs args) |
| 77 | + { |
| 78 | + switch (args.Reason) |
| 79 | + { |
| 80 | + case ToastDismissalReason.ApplicationHidden: |
| 81 | + Debug.WriteLine($"Notification dismissed: Application hidden"); |
| 82 | + break; |
| 83 | + case ToastDismissalReason.UserCanceled: |
| 84 | + Debug.WriteLine($"Notification dismissed: User cancelled"); |
| 85 | + break; |
| 86 | + case ToastDismissalReason.TimedOut: |
| 87 | + Debug.WriteLine($"Notification dismissed: Timed out"); |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static void Notification_Activated(ToastNotification sender, object args) |
| 93 | + { |
| 94 | + |
| 95 | + foreach (Form form in Application.OpenForms) |
| 96 | + { |
| 97 | + if (form.Name == "frmMain") |
| 98 | + { |
| 99 | + form.BeginInvoke((Action)(() => { |
| 100 | + |
| 101 | + form.Show(); |
| 102 | + form.WindowState = FormWindowState.Normal; |
| 103 | + })); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments