From 24141abde220821034a5790fe9d91aabd92afb11 Mon Sep 17 00:00:00 2001 From: Andrey Nekrasov Date: Thu, 17 Dec 2020 19:34:55 +0300 Subject: [PATCH] updating: remove all scheduled toast notifications from previous versions (#8644) --- src/common/notifications/notifications.cpp | 30 ++++++++++++++++++---- src/common/notifications/notifications.h | 3 ++- src/common/updating/notifications.cpp | 14 +++++----- src/runner/main.cpp | 3 ++- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/common/notifications/notifications.cpp b/src/common/notifications/notifications.cpp index 90195b4e5f..ed10ec4ebe 100644 --- a/src/common/notifications/notifications.cpp +++ b/src/common/notifications/notifications.cpp @@ -28,6 +28,9 @@ using namespace winrt::Windows::ApplicationModel::Background; using winrt::Windows::Data::Xml::Dom::XmlDocument; +using winrt::Windows::UI::Notifications::NotificationData; +using winrt::Windows::UI::Notifications::NotificationUpdateResult; +using winrt::Windows::UI::Notifications::ScheduledToastNotification; using winrt::Windows::UI::Notifications::ToastNotification; using winrt::Windows::UI::Notifications::ToastNotificationManager; @@ -396,7 +399,7 @@ void notifications::show_toast_with_activations(std::wstring message, map.Insert(L"progressValueString", std::to_wstring(static_cast(progress * 100)) + std::wstring(L"%")); map.Insert(L"progressTitle", params.progress_bar->progress_title); } - winrt::Windows::UI::Notifications::NotificationData data{ map }; + NotificationData data{ map }; notification.Data(std::move(data)); const auto notifier = winstore::running_as_packaged() ? ToastNotificationManager::ToastNotificationManager::CreateToastNotifier() : @@ -438,14 +441,13 @@ void notifications::update_toast_progress_bar(std::wstring_view tag, progress_ba map.Insert(L"progressValueString", std::to_wstring(static_cast(progress * 100)) + std::wstring(L"%")); map.Insert(L"progressTitle", params.progress_title); - winrt::Windows::UI::Notifications::NotificationData data(map); - winrt::Windows::UI::Notifications::NotificationUpdateResult res = notifier.Update(data, tag, DEFAULT_TOAST_GROUP); + NotificationData data(map); + NotificationUpdateResult res = notifier.Update(data, tag, DEFAULT_TOAST_GROUP); } -void notifications::remove_toasts(std::wstring_view tag) +void notifications::remove_toasts_by_tag(std::wstring_view tag) { using namespace winrt::Windows::System; - try { User currentUser{ *User::FindAllAsync(UserType::LocalUser, UserAuthenticationStatus::LocallyAuthenticated).get().First() }; @@ -455,6 +457,7 @@ void notifications::remove_toasts(std::wstring_view tag) } currentUser.GetPropertyAsync(KnownUserProperties::AccountName()); auto toastHistory = ToastNotificationManager::GetForUser(currentUser).History(); + toastHistory.Remove(tag, DEFAULT_TOAST_GROUP, APPLICATION_ID); } catch (...) @@ -462,3 +465,20 @@ void notifications::remove_toasts(std::wstring_view tag) // Couldn't get the current user or problem removing the toast => nothing we can do } } + +void notifications::remove_all_scheduled_toasts() +{ + const auto notifier = winstore::running_as_packaged() ? ToastNotificationManager::ToastNotificationManager::CreateToastNotifier() : + ToastNotificationManager::ToastNotificationManager::CreateToastNotifier(APPLICATION_ID); + + try + { + for (const auto& scheduled_toast : notifier.GetScheduledToastNotifications()) + { + notifier.RemoveFromSchedule(scheduled_toast); + } + } + catch (...) + { + } +} diff --git a/src/common/notifications/notifications.h b/src/common/notifications/notifications.h index d5cee41bc4..4e4ea6fd9e 100644 --- a/src/common/notifications/notifications.h +++ b/src/common/notifications/notifications.h @@ -62,5 +62,6 @@ namespace notifications void show_toast(std::wstring plaintext_message, std::wstring title, toast_params params = {}); void show_toast_with_activations(std::wstring plaintext_message, std::wstring title, std::wstring_view background_handler_id, std::vector actions, toast_params params = {}); void update_toast_progress_bar(std::wstring_view tag, progress_bar_params params); - void remove_toasts(std::wstring_view tag); + void remove_toasts_by_tag(std::wstring_view tag); + void remove_all_scheduled_toasts(); } diff --git a/src/common/updating/notifications.cpp b/src/common/updating/notifications.cpp index a14f55c665..853fc4f130 100644 --- a/src/common/updating/notifications.cpp +++ b/src/common/updating/notifications.cpp @@ -24,7 +24,7 @@ namespace updating void show_unavailable(const notifications::strings& strings, std::wstring reason) { - remove_toasts(UPDATING_PROCESS_TOAST_TAG); + remove_toasts_by_tag(UPDATING_PROCESS_TOAST_TAG); toast_params toast_params{ UPDATING_PROCESS_TOAST_TAG, false }; show_toast(std::move(reason), strings.TOAST_TITLE, std::move(toast_params)); @@ -32,7 +32,7 @@ namespace updating void show_available(const updating::new_version_download_info& info, const notifications::strings& strings) { - remove_toasts(UPDATING_PROCESS_TOAST_TAG); + remove_toasts_by_tag(UPDATING_PROCESS_TOAST_TAG); toast_params toast_params{ UPDATING_PROCESS_TOAST_TAG, false }; std::wstring contents = strings.GITHUB_NEW_VERSION_AVAILABLE; @@ -51,7 +51,7 @@ namespace updating void show_download_start(const updating::new_version_download_info& info, const notifications::strings& strings) { - remove_toasts(UPDATING_PROCESS_TOAST_TAG); + remove_toasts_by_tag(UPDATING_PROCESS_TOAST_TAG); progress_bar_params progress_bar_params; std::wstring progress_title{ info.version.toWstring() }; @@ -70,7 +70,7 @@ namespace updating void show_visit_github(const updating::new_version_download_info& info, const notifications::strings& strings) { - remove_toasts(UPDATING_PROCESS_TOAST_TAG); + remove_toasts_by_tag(UPDATING_PROCESS_TOAST_TAG); toast_params toast_params{ UPDATING_PROCESS_TOAST_TAG, false }; std::wstring contents = strings.GITHUB_NEW_VERSION_AVAILABLE_OFFER_VISIT; @@ -86,7 +86,7 @@ namespace updating void show_install_error(const updating::new_version_download_info& info, const notifications::strings& strings) { - remove_toasts(UPDATING_PROCESS_TOAST_TAG); + remove_toasts_by_tag(UPDATING_PROCESS_TOAST_TAG); toast_params toast_params{ UPDATING_PROCESS_TOAST_TAG, false }; std::wstring contents = strings.GITHUB_NEW_VERSION_DOWNLOAD_INSTALL_ERROR; @@ -101,7 +101,7 @@ namespace updating void show_version_ready(const updating::new_version_download_info& info, const notifications::strings& strings) { - remove_toasts(UPDATING_PROCESS_TOAST_TAG); + remove_toasts_by_tag(UPDATING_PROCESS_TOAST_TAG); toast_params toast_params{ UPDATING_PROCESS_TOAST_TAG, false }; std::wstring new_version_ready{ strings.GITHUB_NEW_VERSION_READY_TO_INSTALL }; @@ -125,7 +125,7 @@ namespace updating void show_uninstallation_error(const notifications::strings& strings) { - remove_toasts(UPDATING_PROCESS_TOAST_TAG); + remove_toasts_by_tag(UPDATING_PROCESS_TOAST_TAG); show_toast(strings.UNINSTALLATION_UNKNOWN_ERROR, strings.TOAST_TITLE); } diff --git a/src/runner/main.cpp b/src/runner/main.cpp index 2f17c10e3a..98c60f54bf 100644 --- a/src/runner/main.cpp +++ b/src/runner/main.cpp @@ -318,7 +318,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine } case SpecialMode::ReportSuccessfulUpdate: { - notifications::remove_toasts(notifications::UPDATING_PROCESS_TOAST_TAG); + notifications::remove_toasts_by_tag(notifications::UPDATING_PROCESS_TOAST_TAG); + notifications::remove_all_scheduled_toasts(); notifications::show_toast(GET_RESOURCE_STRING(IDS_PT_UPDATE_MESSAGE_BOX_TEXT), L"PowerToys", notifications::toast_params{ notifications::UPDATING_PROCESS_TOAST_TAG });