WindowsAppSDK icon indicating copy to clipboard operation
WindowsAppSDK copied to clipboard

Clicking a notification from Shell_NotifyIcon opens a second instance of a packaged app

Open sylveon opened this issue 1 month ago • 2 comments

Describe the bug

When clicking a notification that a MSIX-packaged app creates, it opens a second instance instead of behaving like unpackaged apps and bringing the app window in the foreground.

Steps to reproduce the bug

  1. Create a new C++ WinUI 3 app
  2. Add the following to MainWindow.xaml.h:
        void InitializeComponent();
        void Button_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e);
    
    private:
        NOTIFYICONDATA m_icon;
    
  3. Add the following to MainWindow.xaml:
    <Grid>
        <Button Click="Button_Click">Send notification</Button>
    </Grid>
    
  4. Add the following to MainWindow.xaml.cpp:
    void winrt::App4::implementation::MainWindow::InitializeComponent()
    {
        MainWindowT::InitializeComponent();
    
        HWND window{};
        winrt::check_hresult(m_inner.as<IWindowNative>()->get_WindowHandle(&window));
    
        m_icon = {
            .cbSize = sizeof(m_icon),
            .hWnd = window,
            .uID = 45678908,
            .uFlags = NIF_MESSAGE | NIF_TIP | NIF_SHOWTIP | NIF_ICON,
            .uCallbackMessage = 0xbeef,
            .hIcon = ExtractIcon(nullptr, L"shell32.dll", 130),
            .szTip = L"test_app",
            .uVersion = NOTIFYICON_VERSION_4
        };
    
        Shell_NotifyIcon(NIM_ADD, &m_icon);
        Shell_NotifyIcon(NIM_SETVERSION, &m_icon);
    }
    
    void winrt::App4::implementation::MainWindow::Button_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e)
    {
        auto icon = m_icon;
        icon.uFlags |= NIF_INFO;
        icon.dwInfoFlags = NIIF_INFO;
        wcscpy_s(icon.szInfo, L"test_app");
        wcscpy_s(icon.szInfoTitle, L"test_app");
    
        BOOL result = Shell_NotifyIcon(NIM_MODIFY, &icon);
    }
    
  5. Edit the project file for unpackaged and launch the app unpackaged
  6. Click button and click notification. Observe no second instance is created.
  7. Edit the project file for packaged and launch the app pckaged.
  8. Click button and click notification. Observe a second instance of the app is created.

Expected behavior

No second instance is created, even when the app is packaged. This was the behavior previously as well

Screenshots

No response

NuGet package version

Windows App SDK 1.8.3: 1.8.251106002

Packaging type

Packaged (MSIX)

Windows version

Insider Build (xxxxx)

IDE

Visual Studio 2022

Additional context

Windows 11 26200.7296

Full repro project here: https://github.com/sylveon/ShellNotifyIcon-packaged

sylveon avatar Nov 24 '25 07:11 sylveon

Previous OS builds like 19041 and 22000

sylveon avatar Nov 24 '25 15:11 sylveon