public static async void GetNotificationsAndProcessThem() { IReadOnlyList <UserNotification> notifs = await MainPage.listener.GetNotificationsAsync(NotificationKinds.Toast); UserNotification n = notifs[notifs.Count - 1]; // Get the toast binding, if present NotificationBinding toastBinding = n.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric); if (toastBinding != null) { // And then get the text elements from the toast binding IReadOnlyList <AdaptiveNotificationText> textElements = toastBinding.GetTextElements(); String appName = n.AppInfo.DisplayInfo.DisplayName; string bodyText = string.Join(" | ", textElements.Select(t => t.Text)); Debug.Write(bodyText); MainPage.items.Add(bodyText); if (ApiInformation.IsApiContractPresent("Windows.ApplicationModel.FullTrustAppContract", 1, 0)) { ApplicationData.Current.LocalSettings.Values["parameters"] = appName + " | " + bodyText; await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync(); } } }
public async Task <List <Notification> > GetNotifications() { IReadOnlyList <UserNotification> notifs = await this._listener.GetNotificationsAsync(NotificationKinds.Toast); List <Notification> notifications = new List <Notification>(); this._allNotifications = new List <uint>(); foreach (UserNotification notif in notifs) { NotificationBinding toastBinding = notif.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric); string displayName = notif.AppInfo.DisplayInfo.DisplayName; string titleText = string.Empty; string bodyText = string.Empty; // get the body text if (toastBinding != null) { // And then get the text elements from the toast binding IReadOnlyList <AdaptiveNotificationText> textElements = toastBinding.GetTextElements(); // Treat the first text element as the title text titleText = textElements.FirstOrDefault()?.Text; // We'll treat all subsequent text elements as body text, // joining them together via newlines. bodyText = string.Join("\n", textElements.Skip(1).Select(t => t.Text)); // if there are generics, like google chrome, we try to split it up if (displayName.ToLower() == "google chrome") { displayName = "gmail"; string[] bodyParts = bodyText.ToLower().Split(" - "); // check if it's in a time format only. if (System.Text.RegularExpressions.Regex.IsMatch(bodyText, @"^[0-9]{1,2}:[0-9]{1,2}(am|pm){1}\x20{1}–\x20{1}[0-9]{1,2}:[0-9]{1,2}(am|pm){1}$")) { displayName = "calendar"; } // assume before the "-" is the sender and after the "-" is the subject. This is a hardcoded exception for // nest. else if (bodyParts.Length > 1 && bodyParts[0] == "noreply" && bodyParts[1].StartsWith("nest notification")) { displayName = "nest"; } } } notifications.Add(new Notification() { NotificationId = notif.Id, DisplayName = displayName, Title = titleText, Body = bodyText, CreationTimestamp = notif.CreationTime.Ticks });; this._allNotifications.Add(notif.Id); } return(notifications); }
private async void Listener_NotificationChanged(UserNotificationListener sender, UserNotificationChangedEventArgs args) { IReadOnlyList <UserNotification> notifs = await sender.GetNotificationsAsync(NotificationKinds.Toast); Debug.WriteLine("--------------NEW Event Received----------------"); foreach (var notif in notifs) { NotificationBinding toastBinding = notif.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric); if (toastBinding != null) { // And then get the text elements from the toast binding IReadOnlyList <AdaptiveNotificationText> textElements = toastBinding.GetTextElements(); // Treat the first text element as the title text string titleText = textElements.FirstOrDefault()?.Text; // We'll treat all subsequent text elements as body text, // joining them together via newlines. string bodyText = string.Join("\n", textElements.Skip(1).Select(t => t.Text)); string payload = "App: " + notif.AppInfo.DisplayInfo.DisplayName + " => Title: " + titleText + ", Body: " + bodyText; Debug.WriteLine(payload); _listItems.Add(new NotificationCenterData() { AppName = notif.AppInfo.DisplayInfo.DisplayName, Title = titleText, Body = bodyText }); } } await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { NotificationListView.ItemsSource = _listItems; }); }
private MiriotNotification ToMiriotNotification(UserNotification notif) { NotificationBinding toastBinding = notif.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric); if (toastBinding != null) { // And then get the text elements from the toast binding IReadOnlyList <AdaptiveNotificationText> textElements = toastBinding.GetTextElements(); // Treat the first text element as the title text string titleText = textElements.FirstOrDefault()?.Text; // We'll treat all subsequent text elements as body text, // joining them together via newlines. string bodyText = string.Join("\n", textElements.Skip(1).Select(t => t.Text)); var mn = new MiriotNotification() { SenderName = titleText, Content = bodyText }; return(mn); } return(null); }
protected override async void OnBackgroundActivated(BackgroundActivatedEventArgs args) { var deferral = args.TaskInstance.GetDeferral(); switch (args.TaskInstance.Task.Name) { case "UserNotificationChanged": // Call your own method to process the new/removed notifications // The next section of documentation discusses this code //await MyWearableHelpers.SyncNotifications(); var frame = (Frame)Window.Current.Content; var page = (MainPage)frame.Content; UserNotificationListener listener = UserNotificationListener.Current; IReadOnlyList <UserNotification> notifs = await listener.GetNotificationsAsync(NotificationKinds.Toast); double x = notifs.Count; if (x > 0) { UserNotification notif = notifs[0]; // Get the app's display name string appDisplayName = notif.AppInfo.DisplayInfo.DisplayName; page.setText(appDisplayName); string message = notif.Notification.ToString(); page.setText(message); string titleText = ""; string bodyText = ""; NotificationBinding toastBinding = notif.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric); if (toastBinding != null) { // And then get the text elements from the toast binding IReadOnlyList <AdaptiveNotificationText> textElements = toastBinding.GetTextElements(); // Treat the first text element as the title text titleText = textElements.FirstOrDefault()?.Text; // We'll treat all subsequent text elements as body text, // joining them together via newlines. bodyText = string.Join("\n", textElements.Skip(1).Select(t => t.Text)); bodyText += ""; } page.setText(titleText); page.setText(bodyText); // Get the app's logo BitmapImage appLogo = new BitmapImage(); RandomAccessStreamReference appLogoStream = notif.AppInfo.DisplayInfo.GetLogo(new Size(16, 16)); await appLogo.SetSourceAsync(await appLogoStream.OpenReadAsync()); } break; } deferral.Complete(); }
private static async Task NowTask() { UserNotificationListener listener = UserNotificationListener.Current; UserNotificationListenerAccessStatus accessStatus = await listener.RequestAccessAsync(); switch (accessStatus) { case UserNotificationListenerAccessStatus.Allowed: Console.WriteLine("notifications allowed"); break; case UserNotificationListenerAccessStatus.Denied: Console.WriteLine("notifications denied"); break; case UserNotificationListenerAccessStatus.Unspecified: Console.WriteLine("notifications something else"); break; } IReadOnlyList <UserNotification> notifs = await listener.GetNotificationsAsync(NotificationKinds.Toast); List <string> combined = new List <string>(); foreach (var notif in notifs) { NotificationBinding toastBinding = notif.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric); if (toastBinding != null) { // And then get the text elements from the toast binding IReadOnlyList <AdaptiveNotificationText> textElements = toastBinding.GetTextElements(); // Treat the first text element as the title text string titleText = textElements.FirstOrDefault()?.Text; // We'll treat all subsequent text elements as body text, // joining them together via newlines. string bodyText = string.Join("\n", textElements.Skip(1).Select(t => t.Text)); //Console.WriteLine("\nhead: " + titleText); //Console.WriteLine("body: " + bodyText); combined.Add(titleText); combined.Add(bodyText); } } Console.WriteLine("\nWritten to: " + Settings.notificationCacheFileLocation); await File.WriteAllLinesAsync(Settings.notificationCacheFileLocation, combined); //clears windows messages | throws an AggregateException, I have no idea why, nothing from google, too //listener.ClearNotifications(); Environment.Exit(0); }
public static void ProcessNotification(UserNotification userNotification) { Debug.WriteLine("Start Saving Notification"); if (userNotification != null) { string appDisplayName = userNotification.AppInfo.DisplayInfo.DisplayName; Debug.WriteLine(appDisplayName); var time = userNotification.CreationTime.DateTime; // Get the toast binding, if present NotificationBinding toastBinding = userNotification.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric); if (toastBinding == null) { return; } //if (toastBinding != null && ) //{ // And then get the text elements from the toast binding IReadOnlyList <AdaptiveNotificationText> textElements = toastBinding.GetTextElements(); // Treat the first text element as the title text foreach (AdaptiveNotificationText s in textElements) { Debug.WriteLine(s.Text); } string titleText = textElements.FirstOrDefault()?.Text; string bodyText = string.Join(" ", textElements.Skip(1).Select(t => t.Text)); if (!isValidRecord(appDisplayName, titleText, bodyText)) { return; } List <String> temp = new List <String>(); temp.Add(time.ToString()); temp.Add(appDisplayName); temp.Add(titleText); temp.Add(bodyText); WriteToFile(recordsFile, string.Join(",", temp)); } Debug.WriteLine("End Saving Notification"); }
public WaNotification(UserNotification userNotification) { string titleText = "", bodyText = ""; NotificationBinding toastBinding = userNotification.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric); if (toastBinding != null) { IReadOnlyList <AdaptiveNotificationText> textElements = toastBinding.GetTextElements(); titleText = textElements.FirstOrDefault()?.Text; bodyText = string.Join("\n", textElements.Skip(1).Select(t => t.Text)); } this.Id = userNotification.Id; this.CreationTime = userNotification.CreationTime; this.Description = userNotification.AppInfo.DisplayInfo.Description; this.DisplayName = userNotification.AppInfo.DisplayInfo.DisplayName; this.TitleText = titleText; this.BodyText = bodyText; }
public static async Task <MyNotification> FromUserNotification(UserNotification notification) { string appId = notification.AppInfo.AppUserModelId; string appName = notification.AppInfo.DisplayInfo.DisplayName; // Get the app's logo WriteableBitmap appLogo = new WriteableBitmap(8, 8); try { var appLogoStream = notification.AppInfo.DisplayInfo.GetLogo(new Size(8, 8)); var stream = await appLogoStream.OpenReadAsync(); await appLogo.SetSourceAsync(stream); } catch (NullReferenceException) { Debug.WriteLine("Error getting BitmapImage!"); } // Get the toast binding, if present NotificationBinding toastBinding = notification.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric); if (toastBinding != null) { IReadOnlyList <AdaptiveNotificationText> textElements = toastBinding.GetTextElements(); string title = textElements.FirstOrDefault()?.Text; string description = string.Join("\n", textElements.Skip(1).Select(t => t.Text)); return(new MyNotification(notification.Id, appId, appName, appLogo, title, description)); } else { return(new MyNotification(notification.Id, appId, appName, appLogo)); } }
private static void SendNotificationToWearable(UserNotification userNotification) { string titleText = ""; string bodyText = ""; // Get the app's display name string appDisplayName = userNotification.AppInfo.DisplayInfo.DisplayName; // Get the toast binding, if present NotificationBinding toastBinding = userNotification.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric); if (toastBinding != null) { // And then get the text elements from the toast binding IReadOnlyList <AdaptiveNotificationText> textElements = toastBinding.GetTextElements(); // Treat the first text element as the title text titleText = textElements.FirstOrDefault()?.Text; // We'll treat all subsequent text elements as body text, // joining them together via newlines. bodyText = string.Join("\n", textElements.Skip(1).Select(t => t.Text)); } // Load settings string settings = LoadSettings(); string[] setting; if (settings != null) { setting = settings.Split(';'); } else { return; } bool ssl = (setting[3] == "1") ? true : false; // https://github.com/jstedfast/MailKit#sending-messages var message = new MimeMessage(); message.To.Add(new MailboxAddress("", setting[0])); message.Subject = $"[Notif2Mail] <{appDisplayName}> {titleText}"; message.Body = new TextPart("plain") { Text = bodyText }; using (var client = new SmtpClient()) { // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS) client.ServerCertificateValidationCallback = (s, c, h, e) => true; client.Connect(setting[1], int.Parse(setting[2]), ssl); // Note: only needed if the SMTP server requires authentication client.Authenticate(setting[4], setting[5]); client.Send(message); client.Disconnect(true); } }
private async Task UpdateNotiFile() { // Check Permissions if (!ApiInformation.IsTypePresent("Windows.UI.Notifications.Management.UserNotificationListener")) { Debug.WriteLine("IsTypePresent: NG"); return; } Debug.WriteLine("IsTypePresent: OK"); UserNotificationListener listener = UserNotificationListener.Current; Debug.Write("listener: "); Debug.WriteLine(listener); while (true) { // Read Notifications try { UserNotificationListenerAccessStatus accessStatus = await listener.RequestAccessAsync(); Debug.Write("accessStatus: "); Debug.WriteLine(accessStatus); if (accessStatus != UserNotificationListenerAccessStatus.Allowed) { Debug.WriteLine("アクセス拒否"); return; } Debug.WriteLine("アクセス許可"); IReadOnlyList <UserNotification> notifs = await listener.GetNotificationsAsync(NotificationKinds.Toast); // Write to Download/App3 Windows.Storage.StorageFile file = await DownloadsFolder.CreateFileAsync("notifications.txt"); // await Windows.Storage.FileIO.WriteTextAsync(file, "Example of writing a string\r\n"); // Append a list of strings, one per line, to the file var listOfStrings = new List <string> { }; foreach (var n in notifs) { NotificationBinding toastBinding = n.Notification.Visual.GetBinding(KnownNotificationBindings.ToastGeneric); if (toastBinding != null) { IReadOnlyList <AdaptiveNotificationText> textElements = toastBinding.GetTextElements(); string titleText = textElements.FirstOrDefault()?.Text; string bodyText = string.Join("\n", textElements.Skip(1).Select(t => t.Text)); Debug.Write("Title:"); Debug.WriteLine(titleText); Debug.Write("Body:"); Debug.WriteLine(bodyText); int MsgType = GetNotiType(n.AppInfo.DisplayInfo.DisplayName); if (MsgType != -1) { listOfStrings.Add(String.Format("{0}", MsgType)); } // await Windows.Storage.FileIO.WriteTextAsync(file, String.Format("{0}, {1}\n", n.AppInfo.DisplayInfo.DisplayName, titleText)); } } await Windows.Storage.FileIO.AppendLinesAsync(file, listOfStrings); // each entry in the list is written to the file on its own line. await Task.Delay(TimeSpan.FromSeconds(1)); } catch (Exception ex) { } } }