Skip to content

Commit

Permalink
updating: remove all scheduled toast notifications from previous vers…
Browse files Browse the repository at this point in the history
…ions (#8644)
  • Loading branch information
yuyoyuppe committed Dec 17, 2020
1 parent a0ccca5 commit 24141ab
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
30 changes: 25 additions & 5 deletions src/common/notifications/notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -396,7 +399,7 @@ void notifications::show_toast_with_activations(std::wstring message,
map.Insert(L"progressValueString", std::to_wstring(static_cast<int>(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() :
Expand Down Expand Up @@ -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<int>(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() };
Expand All @@ -455,10 +457,28 @@ 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 (...)
{
// 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 (...)
{
}
}
3 changes: 2 additions & 1 deletion src/common/notifications/notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<action_t> 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();
}
14 changes: 7 additions & 7 deletions src/common/updating/notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ 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));
}

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;
Expand All @@ -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() };
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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 };
Expand All @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/runner/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit 24141ab

Please sign in to comment.