Home > Enterprise >  Prevent opening the app again when clicking on toast notification
Prevent opening the app again when clicking on toast notification

Time:02-23

I use Microsoft.Toolkit.Uwp.Notifications.ToastContentBuilder to show messages for my users in WinForms, and its work fine, but sometimes if the user click on the toast, the program that show the toast gets launched again, also if the app is closed it's launched again by clicking on the toast.

I want that the toast to just send a message and do nothing by clicking on it. Any help will be appreciated.

Here is my code

new ToastContentBuilder()
   .AddText("testing")
   .AddText("testing").SetToastDuration(ToastDuration.Long).Show();

CodePudding user response:

For a UWP app, you need background activation, but for a WinForms app, as mentioned in the documentations, you need to handle the foreground activation and without showing any UI, close the app.

For desktop applications, background activations are handled the same as foreground activations (your OnActivated event handler will be triggered). You can choose to not show any UI and close your app after handling activation.

So to prevent showing the showing when you click on a toast while the app is closed, follow these steps:

  1. In the main, call ToastNotificationManagerCompat.WasCurrentProcessToastActivated() to check if this instance is a toast-activated instance(the application was not running and has just opened and activated to handle the notification action) then do not show any window, otherwise show the main window.

  2. Also handle ToastNotificationManagerCompat.OnActivated event and check if it's a toast-activated instance, then do nothing and just exit the app.

Example - Stop opening the app again when clicking on toast notification

In the following .NET 6 example, I have shown some message boxes to help you distinguish between the cases that application is toast-activated or was a running instance.

using Microsoft.Toolkit.Uwp.Notifications;
using Windows.Foundation.Collections;
internal static class Program
{
    [STAThread]
    static void Main()
    {
        ApplicationConfiguration.Initialize();
        //Handle when activated by click on notification
        ToastNotificationManagerCompat.OnActivated  = toastArgs =>
        {
            //Get the activation args, if you need those.
            ToastArguments args = ToastArguments.Parse(toastArgs.Argument);
            //Get user input if there's any and if you need those.
            ValueSet userInput = toastArgs.UserInput;
            //if the app instance just started after clicking on a notification 
            if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
            {
                MessageBox.Show("App was not running, "  
                    "but started and activated by click on a notification.");
                Application.Exit();
            }
            else
            {
                MessageBox.Show("App was running, "  
                    "and activated by click on a notification.");
            }
        };
        if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated())
        {
            //Do not show any window
            Application.Run();
        }
        else
        {
            //Show the main form
            Application.Run(new Form1());
        }
    }
}

To learn more:

  • Related