Exemple #1
0
        private async void OnStartup(object sender, StartupEventArgs e)
        {
//{[{
            // https://docs.microsoft.com/windows/uwp/design/shell/tiles-and-notifications/send-local-toast?tabs=desktop
            ToastNotificationManagerCompat.OnActivated += (toastArgs) =>
            {
                Current.Dispatcher.Invoke(async() =>
                {
                    var config = SimpleIoc.Default.GetInstance <IConfiguration>();
                    config[ToastNotificationActivationHandler.ActivationArguments] = toastArgs.Argument;
                    await _host.StartAsync();
                });
            };
//}]}
            _host = SimpleIoc.Default.GetInstance <IApplicationHostService>();
//^^
//{[{
            if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
            {
                // ToastNotificationActivator code will run after this completes and will show a window if necessary.
                return;
            }
//}]}
            await _host.StartAsync();
        }
        /// <summary>
        /// </summary>
        /// <param name="applicationContext"></param>
        public WindowsNotificationManager(WindowsApplicationContext?applicationContext = null)
        {
            _applicationContext  = applicationContext ?? WindowsApplicationContext.FromCurrentProcess();
            _launchActionPromise = new TaskCompletionSource <string>();

#if !NETSTANDARD
            if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
            {
                ToastNotificationManagerCompat.OnActivated += OnAppActivated;

                if (_launchActionPromise.Task.Wait(LaunchNotificationWaitMs))
                {
                    LaunchActionId = _launchActionPromise.Task.Result;
                }
            }
#endif

#if NETSTANDARD
            _toastNotifier = ToastNotificationManager.CreateToastNotifier(_applicationContext.AppUserModelId);
#else
            _toastNotifier = ToastNotificationManagerCompat.CreateToastNotifier();
#endif

            _notifications = new Dictionary <ToastNotification, Notification>();
        }
Exemple #3
0
        public static void InvokeUpdateToast(bool canUseUpdater = true, bool downgrade = false)
        {
            if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
            {
                return;
            }

            string updateString = downgrade ? "Downgrade" : "Update";
            string updateAction = downgrade ? "downgrade" : "update";

            Program.ActionQueue.Add(() =>
            {
                if (canUseUpdater)
                {
                    new ToastContentBuilder()
                    .AddText($"{updateString} {UpdateHandler.UpstreamVersion.Tag} available")
                    .AddText($"Current Version: {Assembly.GetExecutingAssembly().GetName().Version}")
                    .AddText($"Message: {UpdateHandler.UpstreamVersion.Message}")
                    .AddButton(new ToastButton()
                               .SetContent(updateString)
                               .AddArgument("action", updateAction))
                    .SetBackgroundActivation()
                    .AddButton(new ToastButton()
                               .SetContent("Postpone")
                               .AddArgument("action", "postpone"))
                    //.SetBackgroundActivation()
                    //.SetProtocolActivation(new Uri(UpdateInfo.changelogUrl))
                    .SetProtocolActivation(new Uri(UpdateHandler.UpstreamVersion.ChangelogUrl))
                    .Show(toast =>
                    {
                        toast.Tag = "adm_update";
                    });
                }
                else
                {
                    new ToastContentBuilder()
                    .AddText($"{updateString} {UpdateHandler.UpstreamVersion.Tag} available")
                    .AddText($"Current Version: {Assembly.GetExecutingAssembly().GetName().Version}")
                    .AddText($"Message: {UpdateHandler.UpstreamVersion.Message}")
                    .AddButton(new ToastButton()
                               .SetContent("Go to download page")
                               .SetProtocolActivation(new Uri(UpdateHandler.UpstreamVersion.ChangelogUrl)))
                    .SetProtocolActivation(new Uri(UpdateHandler.UpstreamVersion.ChangelogUrl))
                    .Show(toast =>
                    {
                        toast.Tag = "adm_update";
                    });
                }
            });
        }
Exemple #4
0
        protected override void OnStartup(StartupEventArgs e)
        {
            // Listen to toast notification activations
            ToastNotificationManagerCompat.OnActivated += this.ToastNotificationManagerCompat_OnActivated;

            // If launched from a toast
            if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
            {
                // Our OnActivated callback will run after this completes,
                // and will show a window if necessary.
            }
            else
            {
                // Show the window
                // In App.xaml, be sure to remove the StartupUri so that a window doesn't
                // get created by default, since we're creating windows ourselves (and sometimes we
                // don't want to create a window if handling a background activation).
                new MainWindow().Show();
            }
        }
Exemple #5
0
        private async void OnStartup(object sender, StartupEventArgs e)
        {
//{[{
            // https://docs.microsoft.com/windows/uwp/design/shell/tiles-and-notifications/send-local-toast?tabs=desktop
            ToastNotificationManagerCompat.OnActivated += (toastArgs) =>
            {
                Current.Dispatcher.Invoke(async() =>
                {
                    var config = GetService <IConfiguration>();
                    config[ToastNotificationActivationHandler.ActivationArguments] = toastArgs.Argument;
                    await _host.StartAsync();
                });
            };

            // TODO: Register arguments you want to use on App initialization
            var activationArgs = new Dictionary <string, string>
            {
                { ToastNotificationActivationHandler.ActivationArguments, string.Empty }
            };

//}]}
            _host = Host.CreateDefaultBuilder(e.Args)
                    .ConfigureAppConfiguration(c =>
            {
//^^
//{[{
                c.AddInMemoryCollection(activationArgs);
//}]}
            })
                    .ConfigureServices(ConfigureServices)
                    .Build();
//^^
//{[{
            if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
            {
                // ToastNotificationActivator code will run after this completes and will show a window if necessary.
                return;
            }
//}]}
            await _host.StartAsync();
        }
Exemple #6
0
        protected override async void OnInitialized()
        {
//{[{
            // https://docs.microsoft.com/windows/uwp/design/shell/tiles-and-notifications/send-local-toast?tabs=desktop
            ToastNotificationManagerCompat.OnActivated += (toastArgs) =>
            {
                Current.Dispatcher.Invoke(async() =>
                {
                    var config = Container.Resolve <IConfiguration>();

                    // Store ToastNotification arguments in configuration, so you can use them from any point in the app
                    config[App.ToastNotificationActivationArguments] = toastArgs.Argument;

                    App.Current.MainWindow.Show();
                    App.Current.MainWindow.Activate();
                    if (App.Current.MainWindow.WindowState == WindowState.Minimized)
                    {
                        App.Current.MainWindow.WindowState = WindowState.Normal;
                    }

                    await Task.CompletedTask;
                });
            };

            var toastNotificationsService = Container.Resolve <IToastNotificationsService>();

            toastNotificationsService.ShowToastNotificationSample();
//}]}

//^^
//{[{
            if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
            {
                // ToastNotificationActivator code will run after this completes and will show a window if necessary.
                return;
            }
//}]}
            base.OnInitialized();
            await Task.CompletedTask;
        }
Exemple #7
0
        private async void OnStartup(object sender, StartupEventArgs e)
        {
            // https://docs.microsoft.com/windows/uwp/design/shell/tiles-and-notifications/send-local-toast?tabs=desktop
            ToastNotificationManagerCompat.OnActivated += (toastArgs) =>
            {
                Current.Dispatcher.Invoke(async() =>
                {
                    var config = GetService <IConfiguration>();
                    config[ToastNotificationActivationHandler.ActivationArguments] = toastArgs.Argument;
                    await _host.StartAsync();
                });
            };

            // TODO: Register arguments you want to use on App initialization
            var activationArgs = new Dictionary <string, string>
            {
                { ToastNotificationActivationHandler.ActivationArguments, string.Empty }
            };
            var appLocation = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

            // For more information about .NET generic host see  https://docs.microsoft.com/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-3.0
            _host = Host.CreateDefaultBuilder(e.Args)
                    .ConfigureAppConfiguration(c =>
            {
                c.SetBasePath(appLocation);
                c.AddInMemoryCollection(activationArgs);
            })
                    .ConfigureServices(ConfigureServices)
                    .Build();

            if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
            {
                // ToastNotificationActivator code will run after this completes and will show a window if necessary.
                return;
            }

            await _host.StartAsync();
        }
Exemple #8
0
        private async static void Notifications_OnActivated(ToastNotificationActivatedEventArgsCompat e)
        {
            ToastArguments args     = ToastArguments.Parse(e.Argument);
            var            MainForm = Application.OpenForms["Main"] as Main;

            if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
            {
                if (MainForm.InvokeRequired)
                {
                    MainForm.BeginInvoke(new Action(() =>
                    {
                        MetroFramework.MetroMessageBox.Show(MainForm,
                                                            "Process has been activated by toast notification trigger.",
                                                            "Information",
                                                            MessageBoxButtons.OK,
                                                            MessageBoxIcon.Information);
                    }));
                }
                else
                {
                    MetroFramework.MetroMessageBox.Show(MainForm,
                                                        "Process has been activated by toast notification trigger.",
                                                        "Information",
                                                        MessageBoxButtons.OK,
                                                        MessageBoxIcon.Information);
                }

                return;
            }

            if (!args.Contains("Choice"))
            {
                return; //No choice to select then no action required e.g. body tapped
            }

            NotificationChoice NotificationChoice = NotificationChoice.None;

            if (!Enum.TryParse <NotificationChoice>(args["Choice"], out NotificationChoice))
            {
                return; //No valid choice, we return
            }

            switch (Enum.Parse(typeof(NotificationPurpose), args["Action"]))
            {
            case NotificationPurpose.NotificationsSuppression:
            {
                switch (NotificationChoice)
                {
                case NotificationChoice.Yes:
                    Properties.Settings.Default.SuppressNotifications = 2;
                    break;

                case NotificationChoice.No:
                    Properties.Settings.Default.SuppressNotifications = 1;
                    break;

                default: return;
                }

                Properties.Settings.Default.Save();

                break;
            }

            case NotificationPurpose.TargetDiscovery:
            {
                switch (NotificationChoice)
                {
                case NotificationChoice.Show:
                {
                    if (MainForm.InvokeRequired)
                    {
                        MainForm.BeginInvoke(new Action(() =>
                                {
                                    MainForm.Show();
                                    MainForm.WindowState = FormWindowState.Normal;

                                    if (MainForm.TrayIcon.Visible)
                                    {
                                        MainForm.TrayIcon.Visible = false;
                                    }
                                }));
                    }
                    else
                    {
                        MainForm.Show();
                        MainForm.WindowState = FormWindowState.Normal;

                        if (MainForm.TrayIcon.Visible)
                        {
                            MainForm.TrayIcon.Visible = false;
                        }
                    }

                    break;
                }

                case NotificationChoice.Block:
                {
                    if (!string.IsNullOrEmpty(args["DeviceIP"]) && !string.IsNullOrEmpty(args["DeviceMAC"]))
                    {
                        if (MainForm.InvokeRequired)
                        {
                            MainForm.BeginInvoke(new Action(async() =>
                                    {
                                        await MainForm.BlockDevice(args["DeviceIP"], args["DeviceMAC"]);
                                    }));
                        }
                        else
                        {
                            await MainForm.BlockDevice(args["DeviceIP"], args["DeviceMAC"]);
                        }
                    }

                    break;
                }

                case NotificationChoice.Suppress:
                {
                    Properties.Settings.Default.SuppressNotifications = 1;
                    Properties.Settings.Default.Save();
                    break;
                }

                default: return;
                }

                break;
            }

            default: return;
            }
        }