0

I have created a Console Application that shows a Toast Notification. Also, a Shortcut to the start menu was added along with assigning a AppModelUserID to the Toast. The toast comes up on running the application and the action buttons trigger the ToastActivated event (working as expected). Once the timeout expires, the toast notification moves to the Action Center. However, when I try to hit an action button in the action center for the same toast notification, it does not trigger the ToastActivated event.

Followed Is it possible to send Toast notification from console application? to get the toast notification.

public static bool IsAwesome { get { return true; } }   
public NewToastNotification()
{
    string Toast = "<toast launch=\"app-defined-string\">" +
                            "<visual>" +
                             "<binding template =\"ToastGeneric\">" +
                             "<text> Migration Update</text>" +
                              "<text>" +
                               "Your system requires a Restart" +
                              "</text>" +
                             "</binding>" +
                            "</visual>" +
                            "<actions>" +
                             "<action content=\"Restart Now\" arguments=\"restart\"/>" +
                             "<action content=\"Dismiss\" arguments=\"dismiss\"/>" +
                            "</actions>" +
                           "</toast>";

    XmlDocument tileXml = new XmlDocument();
    tileXml.LoadXml(Toast);
    var toast = new ToastNotification(tileXml);


    toast.Activated += ToastActivated;
    toast.Dismissed += ToastDismissed;
    toast.Failed += ToastFailed;

    string APP_ID = "Notification.KEY";

    RegistryKey root = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Notification.KEY", false);
    if (root == null)
    {
        Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Notification.KEY");
        Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\Notification.KEY", true).SetValue("ShowInActionCenter", 1, RegistryValueKind.DWord);
    }

    // Show the toast. Be sure to specify the AppUserModelId on your application's shortcut!
    ToastNotificationManager.CreateToastNotifier(APP_ID).Show(toast);
    Console.ReadKey();
}

public static bool TryCreateShortcut()
{
    String shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Windows\\Start Menu\\Programs\\DisplayToast.lnk";
    if (!File.Exists(shortcutPath))
    {
        InstallShortcut(shortcutPath);
        return true;
    }
    return false;
}

private static void InstallShortcut(String shortcutPath)
{
    // Find the path to the current executable
    String exePath = Process.GetCurrentProcess().MainModule.FileName;
    IShellLinkW newShortcut = (IShellLinkW)new CShellLink();

    // Create a shortcut to the exe
    ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetPath(exePath));
    ShellHelpers.ErrorHelper.VerifySucceeded(newShortcut.SetArguments(""));

    // Open the shortcut property store, set the AppUserModelId property
    IPropertyStore newShortcutProperties = (IPropertyStore)newShortcut;

    string APP_ID = "Notification.KEY";

    using (PropVariant appId = new PropVariant(APP_ID))
    {
        ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutProperties.SetValue(SystemProperties.System.AppUserModel.ID, appId));
        ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutProperties.Commit());
    }

    // Commit the shortcut to disk
    IPersistFile newShortcutSave = (IPersistFile)newShortcut;

    ShellHelpers.ErrorHelper.VerifySucceeded(newShortcutSave.Save(shortcutPath, true));
}

private static void ToastActivated(ToastNotification e, object source)
{
    if (source is ToastActivatedEventArgs selectedToast)
    {
        if (selectedToast.Arguments.Equals("dismiss"))
        {
            Console.WriteLine("Bye, It's dismmissed!");

            Environment.Exit(0);
        }
        else
        {
            Console.WriteLine("Let's launch an event!");

        }
    }
}

private static void ToastDismissed(object source, ToastDismissedEventArgs e)
{
    switch (e.Reason)
    {
        case ToastDismissalReason.ApplicationHidden:
            // Application hid the toast with ToastNotifier.Hide
            Console.WriteLine("Application Hidden");
            break;
        case ToastDismissalReason.UserCanceled:
            Console.WriteLine("User dismissed the toast");
            break;
        case ToastDismissalReason.TimedOut:
            Console.WriteLine("Toast timeout elapsed");
            break;
    }
}

private static void ToastFailed(object source, ToastFailedEventArgs e)
{
    // Check the error code
    var errorCode = e.ErrorCode;
    Console.WriteLine("Error code:{0}", errorCode);
}

static void Main(string[] args)
{
    TryCreateShortcut();

    NewToastNotification newToast = new NewToastNotification();
}

I expect the action buttons on the toast notification in the action center to trigger the ToastActivated event, but the current does nothing when an action button is clicked in action center.

0 Answers0