Toas notification failed to register

Eduardo Gomez Romero 1,355 Reputation points
2025-03-17T12:11:04.34+00:00

I made a NotificationHelper


namespace NanoFlow.Helpers;

public class NotificationHelper {

    public void LaunchToastNotification(string filename, string filepath) {

        ToastNotificationManagerCompat.OnActivated +=
            toastArgs => {

                ToastArguments arguments = ToastArguments.Parse(
                    toastArgs.Argument);

                if(arguments.TryGetValue("action", out string action)) {
                    HandleToastButtonAction(action, filename);
                }

                // Handle the toast button action
                HandleToastButtonAction(action, filename);
            };

        Toast(filename, filepath);
    }

    private void HandleToastButtonAction(string action, string filename) {
        switch(action) {

            case "openFile":
                break;

            case
                "openFolder":
                OpenFolder(Path.GetFullPath(filename));
                break;
            default:
                break;
        }
    }

    private void OpenFolder(string path) {
        if(!Directory.Exists(path)) {
            return;
        }
        // Open file

     }


      // Display the toast notification  

   private static void Toast(string fileName, string filepath) {
        new ToastContentBuilder()
            .AddText("Success")
            .AddText($"'{fileName}' saved successfully.")
            .AddButton(new ToastButton()
                .SetContent("Open File")
                .AddArgument("action", "openFile")
                .AddArgument("fileName", fileName)).
            AddButton(new ToastButton()
                .SetContent("Open Folder")
                .AddArgument("action", "openFolder")
                .AddArgument("filepath", filepath))
            .Show();
    }
}

and call it in my view model

 [RelayCommand]
 async Task SaveToSTLAsync() {
     // Create the dialog
     var fileNameDialog = new ContentDialog {
         Title = "Save Design",
         Content = new StackPanel {
             Children =
             {
                 new TextBlock { Text = "Enter a name for your design:" },
                 new TextBox { PlaceholderText = "MyDesign", Name = "FileNameTextBox" }
             }
         },
         PrimaryButtonText = "Save",
         CloseButtonText = "Cancel",
         XamlRoot = _rootContainer?.XamlRoot
     };


     var result = await fileNameDialog.ShowAsync();

     if(result == ContentDialogResult.Primary) {  

          GetLineData(); // Ensure LineData is updated
         var filePath = ExportSTL(fileName, 5.0);

         _notificationHelper.LaunchToastNotification(fileName,
             filePath);

     }

and it dosent work

I just want to push the open folder button, and it will open the folder

This is winui 3

I tried this

https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/send-local-toast?tabs=desktop-msix

and

https://github.com/andrewleader/App30

Windows development | Windows App SDK
{count} votes

Accepted answer
  1. Anonymous
    2025-03-24T02:01:41.98+00:00

    Hello,

    Welcome to Microsoft Q&A!

    This is a summary for the discussion in the comment. Hope this will help others who are facing the same issue.

    The first thing is that make sure you've created a WinUI3 project with correct version. It is suggested to use Windows App SDK 1.5 or heigher and .NET 6 or higher.

    You could follow the document https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/send-local-toast?tabs=desktop-msix for implementing the notification feature.

    Step1: Install the Microsoft.Toolkit.Uwp.Notifications Nuget package

    Step2: Add toast code in the default button click event.

    new ToastContentBuilder()
        .AddArgument("action", "viewConversation")
        .AddArgument("conversationId", 9813)
        .AddText("Andrew sent you a picture")
        .AddText("Check this out, The Enchantments in Washington!")
        .Show();
    
    

    Step3: Change the manifest like the document. Make sure you've modified the GUID and app Executable. Don't forget to add declaration for xmlns:com and xmlns:desktop.

    Step4: Add code to handle activation.

    ToastNotificationManagerCompat.OnActivated += toastArgs =>
    {
        // Obtain the arguments from the notification
        ToastArguments args = ToastArguments.Parse(toastArgs.Argument);
    
        // Obtain any user input (text boxes, menu selections) from the notification
        ValueSet userInput = toastArgs.UserInput;
    
        // Need to dispatch to UI thread if performing UI operations
        Application.Current.Dispatcher.Invoke(delegate
        {
            // TODO: Show the corresponding content
            MessageBox.Show("Toast activated. Args: " + toastArgs.Argument);
        });
    };
    
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.