private async Task NotifyFileFinished() { await App.RunOnUiThreadAsync(() => { var filePath = _currentFilePath; App.SystemNotifier.DismissNotification(_currentNofication.Tag); _currentNofication = null; byte[] fileIconData = null; var fileIcon = Icon.ExtractAssociatedIcon(_currentFilePath); if (fileIcon != null) { var fileIconBitmap = fileIcon.ToBitmap(); using (var memoryStream = new MemoryStream()) { fileIconBitmap.Save(memoryStream, ImageFormat.Png); fileIconData = memoryStream.ToArray(); } } var notification = new SystemNotification { Title = _currentFileName, Text = string.Format(Resources.FileReceivedFrom, _remoteDevice.Name), IconData = fileIconData, Actions = new [] { new SystemNotification.Action { Index = 1, Title = Resources.Open }, new SystemNotification.Action { Index = 2, Title = Resources.ShowInFolder } } }; notification.ActionActivated += (_, index, text) => { switch (index) { case 1: Process.Start(filePath); break; case 2: Process.Start("explorer.exe", "/select, \"" + filePath + "\""); break; } }; App.SystemNotifier.ShowNotification(notification); }); }
public void ShowNotification(SystemNotification notification) { ToastTemplateType toastTemplateType; if (notification.IconData != null) { toastTemplateType = notification.AppName != null ? ToastTemplateType.ToastImageAndText04 : ToastTemplateType.ToastImageAndText02; } else { toastTemplateType = notification.AppName != null ? ToastTemplateType.ToastText04 : ToastTemplateType.ToastText02; } var toastXml = ToastNotificationManager.GetTemplateContent(toastTemplateType); var toastStrings = toastXml.GetElementsByTagName("text"); toastStrings[0].AppendChild(toastXml.CreateTextNode(notification.Title)); toastStrings[1].AppendChild(toastXml.CreateTextNode(notification.Text)); if (notification.AppName != null) { toastStrings[2].AppendChild(toastXml.CreateTextNode(notification.AppName)); ((XmlElement)toastStrings[2]).SetAttribute("placement", "attribution"); } if (notification.IconData != null) { var toastImages = toastXml.GetElementsByTagName("image"); var fileName = CreateIconFile(notification.IconData); toastImages[0].Attributes.GetNamedItem("src").NodeValue = "file:///" + fileName; } if (notification.Timestamp != null && Math.Abs((DateTime.Now - notification.Timestamp.Value).TotalMinutes) > 1) { toastXml.DocumentElement.SetAttribute("displayTimestamp", notification.Timestamp.Value.ToString("yyyy-MM-ddTHH:mm:ssZ")); } if (notification.Tag == null) { notification.Tag = "#" + _toastIdGenerator.GetId(notification, out _); } if (notification.Ongoing) { toastXml.DocumentElement.SetAttribute("scenario", "reminder"); if (notification.Actions.Length == 0) { notification.Actions = new[] { new SystemNotification.Action { Index = -1, Title = Resources.Dismiss } }; } } if (notification.Actions.Length > 0) { var actionsElement = toastXml.CreateElement("actions"); foreach (var action in notification.Actions) { var actionElement = toastXml.CreateElement("action"); if (action.IsTextInput) { var inputElement = toastXml.CreateElement("input"); inputElement.SetAttribute("id", "input" + action.Index); inputElement.SetAttribute("type", "text"); inputElement.SetAttribute("placeHolderContent", action.Title); actionsElement.AppendChild(inputElement); actionElement.SetAttribute("hint-inputId", "input" + action.Index); actionElement.SetAttribute("content", Resources.Send); } else { actionElement.SetAttribute("content", action.Title); } var arguments = $"tag={Uri.EscapeDataString(notification.Tag)}&{(action.IsTextInput ? "text-input" : "click")}={action.Index}"; actionElement.SetAttribute("arguments", arguments); actionElement.SetAttribute("activationType", "background"); actionsElement.AppendChild(actionElement); } toastXml.DocumentElement.AppendChild(actionsElement); } var toast = new ToastNotification(toastXml) { Tag = notification.Tag }; toast.Dismissed += OnDismissed; if (_toasts.TryGetValue(notification.Tag, out var oldToast)) { try { _toastNotifier.Hide(oldToast); } catch (Exception e) { App.ShowException(e, true); } } _toasts[notification.Tag] = toast; _notifications[notification.Tag] = notification; try { _toastNotifier.Show(toast); } catch (Exception e) { App.ShowException(e); } }
private async Task <bool> DoSendFile(RemoteDevice.Connection connection, string path) { try { using (var fileStream = new FileStream(path, FileMode.Open)) { var fileName = Path.GetFileName(path); await connection.SendJson(new JObject { ["type"] = "file", ["name"] = fileName, ["size"] = fileStream.Length }); var notification = new SystemNotification { Title = fileName, Text = string.Format(Properties.Resources.SendingFileTo, _device.Name) }; await App.RunOnUiThreadAsync(() => { App.SystemNotifier.ShowNotification(notification); }); try { var buffer = new byte[SendBufferSize]; int count; do { count = await fileStream.ReadAsync(buffer, 0, buffer.Length); if (_cancel) { throw new CancellationException(); } var base64String = Convert.ToBase64String(buffer, 0, count); await connection.SendJson(new JObject { ["type"] = "file", ["chunk"] = base64String }); } while (count == buffer.Length); await connection.SendJson(new JObject { ["type"] = "file", ["status"] = "complete" }); } catch (Exception) { try { await connection.SendJson(new JObject { ["type"] = "file", ["status"] = "cancel" }); } catch (Exception) { // Ignore } throw; } finally { await App.RunOnUiThreadAsync(() => { App.SystemNotifier.DismissNotification(notification.Tag); }); } await App.RunOnUiThreadAsync(() => { App.SystemNotifier.ShowNotification(new SystemNotification { Title = fileName, Text = string.Format(Properties.Resources.FileSentTo, _device.Name) }); }); } } catch (IOException e) { await App.RunOnUiThreadAsync(() => { App.SystemNotifier.ShowNotification(new SystemNotification { Title = _device.Name, Text = string.Format(Properties.Resources.UnableToSendFile, e.Message) }); }); return(false); } catch (CancellationException) { return(false); } return(true); }
public async Task HandleJson(RemoteDevice.Connection connection, dynamic json) { var device = connection.RemoteDevice; string action = json.action; switch (action) { case "posted": { string key = json.key; long timestamp = json.timestamp; string appName = json.appName; string title = json.title; string message = json.message; string base64Icon = json.icon; JArray actions = json.actions; var pngIcon = base64Icon != null?Convert.FromBase64String(base64Icon) : null; var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); dateTime = dateTime.AddMilliseconds(timestamp); await Application.Current.Dispatcher.InvokeAsync(() => { BitmapImage icon = null; if (pngIcon != null) { using (var stream = new MemoryStream(pngIcon)) { icon = new BitmapImage(); icon.BeginInit(); icon.StreamSource = stream; icon.CacheOption = BitmapCacheOption.OnLoad; icon.EndInit(); } } var item = new NotificationItem(key, appName, title, message, icon, dateTime) { Actions = actions.Select(act => { dynamic a = act; int index = a.index; string aTitle = a.title; bool isTextInput = a.isTextInput; return(new NotificationItem.Action(index, aTitle, isTextInput)); }).ToArray() }; NotificationItem oldNotificationItem = null; int i; for (i = 0; i < _notifications.Count; i++) { if (_notifications[i].Key != item.Key) { continue; } oldNotificationItem = _notifications[i]; _notifications[i] = item; break; } if (i == _notifications.Count) { _notifications.Add(item); } var systemNotification = new SystemNotification { AppName = item.AppName, Title = item.Title, Text = item.Message, IconData = pngIcon, Timestamp = item.Timestamp, Actions = item.Actions.Select(act => new SystemNotification.Action { Index = act.Index, Title = act.Title, IsTextInput = act.IsTextInput }).ToArray() }; /* systemNotification.Activated += sender => { * if (Settings.Default.DismissNotificationsByClick) { * Task.Run(async () => { * await connection.SendJson(new JObject { * ["type"] = "notification", * ["key"] = key * }); * }); * } else { * App.ShowDeviceWindow(device); * } * }; */ systemNotification.ActionActivated += (sender, index, text) => { Task.Run(async() => { await connection.SendJson(new JObject { ["type"] = "notification", ["key"] = key, ["actionIndex"] = index, ["actionText"] = text }); }); }; if (oldNotificationItem?.SystemNotificationTag != null) { systemNotification.Tag = oldNotificationItem.SystemNotificationTag; } App.SystemNotifier.ShowNotification(systemNotification); item.SystemNotificationTag = systemNotification.Tag; }); break; } case "removed": { string key = json.key; await Application.Current.Dispatcher.InvokeAsync(() => { for (var i = 0; i < _notifications.Count; i++) { var notification = _notifications[i]; if (notification.Key != key) { continue; } _notifications.RemoveAt(i); if (notification.SystemNotificationTag != null) { App.SystemNotifier.DismissNotification(notification.SystemNotificationTag); } break; } }); break; } } }