### Uninstall Application and Clean Up Registration (C++/WinRT) Source: https://context7.com/windowsnotifications/desktop-toasts/llms.txt Removes all application-related entries from the system, including scheduled notifications, current notifications in Action Center, registry entries, and Start menu shortcut associations. This function ensures a clean uninstallation process. ```cpp #include "DesktopNotificationManagerCompat.h" #include // Perform complete cleanup when uninstalling the application void uninstallApplication() { try { std::cout << "Uninstalling application and cleaning up notifications...\n"; // This will: // 1. Remove all scheduled toast notifications // 2. Clear all current notifications from Action Center // 3. Delete registry keys for AUMID and COM activator // 4. Remove Start menu shortcut associations DesktopNotificationManagerCompat::Uninstall(); std::cout << "Application uninstalled successfully\n"; std::cout << "All notifications and registry entries removed\n"; } catch (const std::exception& ex) { std::cerr << "Uninstall failed: " << ex.what() << "\n"; std::cerr << "Some cleanup may need to be performed manually\n"; } } // Example uninstall command in application menu void handleUninstallCommand() { std::cout << "Are you sure you want to uninstall? (y/n): "; char response; std::cin >> response; if (response == 'y' || response == 'Y') { uninstallApplication(); exit(0); } } ``` -------------------------------- ### COM Activation Registration for Desktop Notifications (C++/WRL) Source: https://context7.com/windowsnotifications/desktop-toasts/llms.txt This C++ code snippet, using WRL, shows how to register a COM server for desktop notifications and implement the INotificationActivationCallback interface. It handles various activation scenarios, including replies and foreground actions, and manages the application's main window. This is suitable for classic Win32 applications. ```cpp #include "DesktopNotificationManagerCompat.h" #include "NotificationActivationCallback.h" #include #include using namespace Microsoft::WRL; using namespace ABI::Windows::UI::Notifications; // Define the COM activator class with specific CLSID // This GUID must match what's registered in the installer class DECLSPEC_UUID("23A5B06E-20BB-4E7E-A0AC-6982ED6A6041") NotificationActivator WrlSealed : public RuntimeClass, INotificationActivationCallback> WrlFinal { public: virtual HRESULT STDMETHODCALLTYPE Activate( _In_ LPCWSTR appUserModelId, _In_ LPCWSTR invokedArgs, _In_reads_(dataCount) const NOTIFICATION_USER_INPUT_DATA* data, ULONG dataCount) override { std::wstring arguments(invokedArgs); HRESULT hr = S_OK; // Handle background reply action if (arguments.find(L"action=reply") == 0) { // Extract user input from text box LPCWSTR response = data[0].Value; hr = SendBasicToast(response); } // Handle background like action else if (arguments.find(L"action=like") == 0) { hr = SendBasicToast(L"Sending like..."); } // Handle foreground activations else { // Open or activate the main window hr = DesktopToastsApp::GetInstance()->OpenWindowIfNeeded(); if (SUCCEEDED(hr)) { if (arguments.find(L"action=viewImage") == 0) { DesktopToastsApp::GetInstance()->SetMessage( L"The user wants to view the image."); } else if (arguments.find(L"action=viewConversation") == 0) { DesktopToastsApp::GetInstance()->SetMessage( L"The user wants to view the conversation."); } } } return S_OK; } ~NotificationActivator() { // Exit if no window is open after handling activation if (!DesktopToastsApp::GetInstance()->HasWindow()) { exit(0); } } }; CoCreatableClass(NotificationActivator); // Main entry point with COM registration int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE, _In_ LPWSTR cmdLineArgs, _In_ int) { RoInitializeWrapper winRtInitializer(RO_INIT_MULTITHREADED); if (FAILED(winRtInitializer)) { return winRtInitializer; } // Register AUMID and COM server (no-op for Desktop Bridge apps) HRESULT hr = DesktopNotificationManagerCompat::RegisterAumidAndComServer( L"WindowsNotifications.DesktopToastsCpp", __uuidof(NotificationActivator) ); if (FAILED(hr)) { return hr; } // Register the COM activator type hr = DesktopNotificationManagerCompat::RegisterActivator(); if (FAILED(hr)) { return hr; } // Create and run the application DesktopToastsApp app; app.SetHInstance(hInstance); std::wstring cmdLineArgsStr(cmdLineArgs); // Check if launched from toast if (cmdLineArgsStr.find(TOAST_ACTIVATED_LAUNCH_ARG) != std::string::npos) { // NotificationActivator will handle the activation } else { // Normal launch hr = app.Initialize(hInstance); if (FAILED(hr)) { return hr; } } app.RunMessageLoop(); return 0; } ``` -------------------------------- ### Send Toast Notifications (C++/WinRT) Source: https://context7.com/windowsnotifications/desktop-toasts/llms.txt This function demonstrates sending a toast notification using C++/WinRT. It constructs the toast's XML content, including text, images, and action buttons, then displays it using `DesktopNotificationManagerCompat::CreateToastNotifier().Show()`. Proper error handling is included. ```cpp #include "DesktopNotificationManagerCompat.h" #include #include #include using namespace winrt; using namespace Windows::Data::Xml::Dom; using namespace Windows::UI::Notifications; void sendToast() { try { std::cout << "Sending a toast...\n"; // Construct the toast XML template XmlDocument doc; doc.LoadXml(L"\ "); // Populate toast with launch arguments doc.DocumentElement().SetAttribute(L"launch", L"action=viewConversation&conversationId=9813"); // Set text content doc.SelectSingleNode(L"//text[1]").InnerText(L"Andrew sent you a picture"); doc.SelectSingleNode(L"//text[2]").InnerText(L"Check this out, Happy Canyon in Utah!"); // Set image sources (can use HTTP URLs for packaged apps) doc.SelectSingleNode(L"//image[1]").as().SetAttribute(L"src", L"https://unsplash.it/64?image=1005"); doc.SelectSingleNode(L"//image[2]").as().SetAttribute(L"src", L"https://picsum.photos/364/202?image=883"); // Set button arguments doc.SelectSingleNode(L"//action[1]").as().SetAttribute(L"arguments", L"action=reply&conversationId=9813"); doc.SelectSingleNode(L"//action[2]").as().SetAttribute(L"arguments", L"action=like&conversationId=9813"); doc.SelectSingleNode(L"//action[3]").as().SetAttribute(L"arguments", L"action=viewImage&imageUrl=https://picsum.photos/364/202?image=883"); // Create the toast notification ToastNotification notif{ doc }; // Get notifier and display the toast DesktopNotificationManagerCompat::CreateToastNotifier().Show(notif); std::cout << "Toast sent successfully!\n"; } catch (const std::exception& ex) { std::cerr << "Failed to send toast: " << ex.what() << "\n"; } } ``` -------------------------------- ### Handle Toast Activation Events (C++/WinRT) Source: https://context7.com/windowsnotifications/desktop-toasts/llms.txt Registers a callback to handle toast activation events, allowing the app to respond to user interactions with notifications. The callback receives arguments and user input, enabling different actions to be triggered based on button clicks or text input. Requires DesktopNotificationManagerCompat and winrt headers. ```cpp #include "DesktopNotificationManagerCompat.h" #include #include using namespace winrt; using namespace Windows::Foundation::Collections; bool _hasStarted = false; void sendBasicToast(std::wstring message); void start(); void showWindow(); int main(int argc, char* argv[]) { // Register the app first DesktopNotificationManagerCompat::Register( L"Microsoft.SampleCppWinRtApp", L"Sample C++ WinRT App", L"C:\\MyIcon.png" ); // Set up activation handler to process toast interactions DesktopNotificationManagerCompat::OnActivated( [](DesktopNotificationActivatedEventArgsCompat e) { std::wstring args = e.Argument(); // Handle "like" button - background activation if (args._Starts_with(L"action=like")) { sendBasicToast(L"Sent like!"); // Exit if app wasn't running before if (!_hasStarted) { exit(0); } } // Handle "reply" button with user input else if (args._Starts_with(L"action=reply")) { // Extract text from input field std::wstring msg = e.UserInput().Lookup(L"tbReply").c_str(); sendBasicToast(L"Sent reply! Reply: " + msg); if (!_hasStarted) { exit(0); } } // Handle foreground activations else { if (!_hasStarted) { // App launched from toast if (args._Starts_with(L"action=viewConversation")) { std::cout << "Launched from toast, opening the conversation!\n\n"; } start(); } else { // App already running - bring to foreground showWindow(); if (args._Starts_with(L"action=viewConversation")) { std::cout << "\n\nOpening the conversation!\n\n"; } } } } ); // Check if launched from toast activation if (argc >= 2 && strcmp(argv[1], TOAST_ACTIVATED_LAUNCH_ARG) == 0) { // Wait for COM activation callback std::cin.ignore(); } else { // Normal launch start(); } return 0; } ``` -------------------------------- ### Create Rich Toast Notifications using ToastContentBuilder (C#) Source: https://context7.com/windowsnotifications/desktop-toasts/llms.txt Constructs rich toast notifications with text, images, input fields, and action buttons using a fluent API. This simplifies the creation of complex toast XML. It requires the Microsoft.Toolkit.Uwp.Notifications NuGet package. ```csharp using Microsoft.Toolkit.Uwp.Notifications; using System; using System.Windows; // Create and display a rich toast notification with images, text input, and buttons private async void ButtonPopToast_Click(object sender, RoutedEventArgs e) { string title = "Andrew sent you a picture"; string content = "Check this out, The Enchantments!"; string image = "https://picsum.photos/364/202?image=883"; int conversationId = 5; try { // Build and display the toast new ToastContentBuilder() // Arguments passed when user taps the toast body .AddArgument("action", "viewConversation") .AddArgument("conversationId", conversationId) // Title and body text .AddText(title) .AddText(content) // Download and add inline image (required for non-packaged apps) .AddInlineImage(new Uri(await DownloadImageToDisk(image))) // Circular app logo overlay .AddAppLogoOverride(new Uri(await DownloadImageToDisk("https://unsplash.it/64?image=1005")), ToastGenericAppLogoCrop.Circle) // Text input box for quick replies .AddInputTextBox("tbReply", "Type a response") // Action buttons with arguments .AddButton(new ToastButton() .SetContent("Reply") .AddArgument("action", "reply")) .AddButton(new ToastButton() .SetContent("Like") .AddArgument("action", "like")) .AddButton(new ToastButton() .SetContent("View") .AddArgument("action", "viewImage") .AddArgument("imageUrl", image)) // Display the toast immediately .Show(); } catch (Exception ex) { MessageBox.Show($"Failed to show toast: {ex.Message}"); } } ``` -------------------------------- ### Register Win32 App for Desktop Notifications (C++/WinRT) Source: https://context7.com/windowsnotifications/desktop-toasts/llms.txt Registers a classic Win32 application with the Windows notification system. This involves creating registry entries for the AUMID, display name, and COM activator, which is essential for non-packaged desktop apps to send and receive toast notifications. Requires the DesktopNotificationManagerCompat header. ```cpp #include "DesktopNotificationManagerCompat.h" #include int main(int argc, char* argv[]) { try { // Register the app with AUMID, display name, and icon path // AUMID: Unique identifier for your app (reverse domain format recommended) // Display Name: Shown in notification settings // Icon Path: Absolute path to app icon (can be empty) DesktopNotificationManagerCompat::Register( L"Microsoft.SampleCppWinRtApp", // AUMID L"Sample C++ WinRT App", // Display Name L"C:\\Program Files\\MyApp\\MyIcon.png" // Icon Path ); std::cout << "App registered successfully\n"; // Now you can send toast notifications // Registration persists in registry until uninstalled } catch (const std::exception& ex) { std::cerr << "Registration failed: " << ex.what() << "\n"; return 1; } return 0; } ``` -------------------------------- ### Handle Toast Interactions with OnActivated (C#) Source: https://context7.com/windowsnotifications/desktop-toasts/llms.txt The OnActivated event handler captures user interactions with toast notifications, allowing access to action arguments and user input. It supports both foreground activations (opening windows) and background activations (quick actions). ```csharp using Microsoft.Toolkit.Uwp.Notifications; using Microsoft.QueryStringDotNET; using System.Windows; protected override void OnStartup(StartupEventArgs e) { // Register the activation handler before showing any toasts ToastNotificationManagerCompat.OnActivated += ToastNotificationManagerCompat_OnActivated; // Check if launched from a toast if (ToastNotificationManagerCompat.WasCurrentProcessToastActivated()) { // NotificationActivator will run and show window if needed } else { // Normal launch - show the main window new MainWindow().Show(); } base.OnStartup(e); } private void ToastNotificationManagerCompat_OnActivated(ToastNotificationActivatedEventArgsCompat e) { // Parse the query string arguments var args = ToastArguments.Parse(e.Argument); Application.Current.Dispatcher.Invoke(delegate { // Handle different actions based on arguments switch (args.Get("action", "")) { case "viewImage": // Foreground: Open window and display image string imageUrl = args["imageUrl"]; OpenWindowIfNeeded(); (App.Current.Windows[0] as MainWindow).ShowImage(imageUrl); break; case "viewConversation": // Foreground: Open window and show conversation int conversationId = int.Parse(args["conversationId"]); OpenWindowIfNeeded(); (App.Current.Windows[0] as MainWindow).ShowConversation(); break; case "reply": // Background: Send quick reply without showing window string msg = e.UserInput["tbReply"] as string; ShowToast("Sending message: " + msg); // Exit if no windows are open (background activation) if (App.Current.Windows.Count == 0) { Application.Current.Shutdown(); } break; case "like": // Background: Send like without showing window ShowToast("Sending like"); if (App.Current.Windows.Count == 0) { Application.Current.Shutdown(); } break; default: OpenWindowIfNeeded(); break; } }); } ``` -------------------------------- ### Manage Notification History (C++/WinRT) Source: https://context7.com/windowsnotifications/desktop-toasts/llms.txt Provides functions to interact with the notification history in Action Center. This includes clearing all notifications, removing specific notifications by tag and/or group, and retrieving the entire notification history. It utilizes the `DesktopNotificationManagerCompat::History()` class. ```cpp #include "DesktopNotificationManagerCompat.h" #include // Clear all notifications from Action Center void clearAllNotifications() { try { auto history = DesktopNotificationManagerCompat::History(); history.Clear(); std::cout << "All notifications cleared\n"; } catch (const std::exception& ex) { std::cerr << "Failed to clear notifications: " << ex.what() << "\n"; } } // Remove specific notification by tag void removeNotificationByTag() { try { auto history = DesktopNotificationManagerCompat::History(); history.Remove(L"message-123"); std::cout << "Notification removed\n"; } catch (const std::exception& ex) { std::cerr << "Failed to remove notification: " << ex.what() << "\n"; } } // Remove notification by tag and group void removeNotificationByTagAndGroup() { try { auto history = DesktopNotificationManagerCompat::History(); history.Remove(L"message-123", L"conversations"); std::cout << "Notification removed from group\n"; } catch (const std::exception& ex) { std::cerr << "Failed to remove notification: " << ex.what() << "\n"; } } // Remove entire group of notifications void removeNotificationGroup() { try { auto history = DesktopNotificationManagerCompat::History(); history.RemoveGroup(L"conversations"); std::cout << "Notification group removed\n"; } catch (const std::exception& ex) { std::cerr << "Failed to remove group: " << ex.what() << "\n"; } } // Get all notification history void getNotificationHistory() { try { auto history = DesktopNotificationManagerCompat::History(); auto notifications = history.GetHistory(); std::cout << "Total notifications: " << notifications.Size() << "\n"; for (uint32_t i = 0; i < notifications.Size(); i++) { auto notif = notifications.GetAt(i); // Process each notification } } catch (const std::exception& ex) { std::cerr << "Failed to get history: " << ex.what() << "\n"; } } ``` -------------------------------- ### Manage Toast History with History (C#) Source: https://context7.com/windowsnotifications/desktop-toasts/llms.txt The History property allows applications to manage their toast notification history, providing methods to clear all notifications or remove specific ones using tag and group identifiers. ```csharp using Microsoft.Toolkit.Uwp.Notifications; using System.Windows; // Clear all toasts for this application from Action Center private void ButtonClearToasts_Click(object sender, RoutedEventArgs e) { try { // Remove all notifications from Action Center ToastNotificationManagerCompat.History.Clear(); MessageBox.Show("All notifications cleared successfully"); } catch (Exception ex) { MessageBox.Show($"Failed to clear notifications: {ex.Message}"); } } // Remove a specific notification by tag private void RemoveSpecificNotification() { try { string tag = "message-123"; ToastNotificationManagerCompat.History.Remove(tag); } catch (Exception ex) { MessageBox.Show($"Failed to remove notification: {ex.Message}"); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.