MicrosoftDocs / winrt-api

WinRT reference content for developing Microsoft Universal Windows Platform (UWP) apps
Creative Commons Attribution 4.0 International
231 stars 500 forks source link

Toast doesn't pop up after calling ToastNotifier.Show #2430

Open autumngao opened 12 months ago

autumngao commented 12 months ago

[Enter feedback here]

I create a consle application based on C++\Winrt , here is the source code.

int main(int argc, char* argv[])
{
    init_apartment();
    // Construct the toast template
    WCHAR szToast[1024] = { 0 };
    LoadString(GetModuleHandle(NULL), IDS_TOAST_MESSAGE, szToast, sizeof(szToast) / sizeof(WCHAR));
    winrt::param::hstring hStr(szToast);
    XmlDocument doc;
    doc.LoadXml(hStr);

    // Construct the notification
    ToastNotification notif{ doc };

    // And send it!
    auto notifier = ToastNotificationManager::CreateToastNotifier(_win32Aumid);

    auto notifierSetting = notifier.Setting();

    switch (notifierSetting)
    {
    case winrt::Windows::UI::Notifications::NotificationSetting::Enabled:
        notifier.Show(notif);
        break;
    case winrt::Windows::UI::Notifications::NotificationSetting::DisabledForApplication:
        break;
    case winrt::Windows::UI::Notifications::NotificationSetting::DisabledForUser:
        break;
    case winrt::Windows::UI::Notifications::NotificationSetting::DisabledByGroupPolicy:
        break;
    case winrt::Windows::UI::Notifications::NotificationSetting::DisabledByManifest:
        break;
    default:
        break;
    }

    return 0;
}

This code works well on windows11, but does not work on windows 10. If I add some operation code before return 0, such as cout << "what ever"<<endl; or sleep(100);, it can work on windows 10 too. It seems the application should wait for a moment after calling ToastNotifier.Show on windows 10.

Why does this happen? Is there any way to get the result of calling ToastNotifier.Show?

Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

autumngao commented 11 months ago

This is my toast template:

<toast>
    <visual>
        <binding template="ToastGeneric">
            <text>Title</text>
            <text>Text</text>
        </binding>
    </visual>
    <actions>
        <action content="Learn more" activationType="protocol" arguments="actionString:test"/>
    </actions>
</toast>