notifications: use message/title directly instead of binding to it, because it breaks snoozed notifications (#8609)

This commit is contained in:
Andrey Nekrasov
2020-12-15 19:59:56 +03:00
committed by GitHub
parent 212ea2de30
commit 445c32c996
2 changed files with 9 additions and 20 deletions

View File

@@ -282,9 +282,16 @@ void notifications::show_toast_with_activations(std::wstring message,
std::wstring toast_xml; std::wstring toast_xml;
toast_xml.reserve(2048); toast_xml.reserve(2048);
// We must set toast's title and contents immediately, because some of the toasts we send could be snoozed.
// Windows instantiates the snoozed toast from scratch before showing it again, so all bindings that were set
// using NotificationData would be empty.
toast_xml += LR"(<?xml version="1.0"?><toast><visual><binding template="ToastGeneric">)"; toast_xml += LR"(<?xml version="1.0"?><toast><visual><binding template="ToastGeneric">)";
toast_xml += LR"(<text id="1">{title}</text>)"; toast_xml += LR"(<text id="1">)";
toast_xml += LR"(<text id="2">{message}</text>)"; toast_xml += std::move(title);
toast_xml += LR"(</text>)";
toast_xml += LR"(<text id="2">)";
toast_xml += std::move(message);
toast_xml += LR"(</text>)";
if (params.progress_bar.has_value()) if (params.progress_bar.has_value())
{ {
@@ -382,8 +389,6 @@ void notifications::show_toast_with_activations(std::wstring message,
notification.Group(DEFAULT_TOAST_GROUP); notification.Group(DEFAULT_TOAST_GROUP);
winrt::Windows::Foundation::Collections::StringMap map; winrt::Windows::Foundation::Collections::StringMap map;
map.Insert(L"message", std::move(message));
map.Insert(L"title", std::move(title));
if (params.progress_bar.has_value()) if (params.progress_bar.has_value())
{ {
float progress = std::clamp(params.progress_bar->progress, 0.0f, 1.0f); float progress = std::clamp(params.progress_bar->progress, 0.0f, 1.0f);
@@ -437,21 +442,6 @@ void notifications::update_toast_progress_bar(std::wstring_view tag, progress_ba
winrt::Windows::UI::Notifications::NotificationUpdateResult res = notifier.Update(data, tag, DEFAULT_TOAST_GROUP); winrt::Windows::UI::Notifications::NotificationUpdateResult res = notifier.Update(data, tag, DEFAULT_TOAST_GROUP);
} }
void notifications::update_toast_contents(std::wstring_view tag, std::wstring plaintext_message, std::wstring title)
{
const auto notifier = winstore::running_as_packaged() ?
ToastNotificationManager::ToastNotificationManager::CreateToastNotifier() :
ToastNotificationManager::ToastNotificationManager::CreateToastNotifier(APPLICATION_ID);
winrt::Windows::Foundation::Collections::StringMap map;
map.Insert(L"title", std::move(title));
map.Insert(L"message", std::move(plaintext_message));
winrt::Windows::UI::Notifications::NotificationData data(map);
winrt::Windows::UI::Notifications::NotificationUpdateResult res = notifier.Update(data, tag, DEFAULT_TOAST_GROUP);
}
void notifications::remove_toasts(std::wstring_view tag) void notifications::remove_toasts(std::wstring_view tag)
{ {
using namespace winrt::Windows::System; using namespace winrt::Windows::System;

View File

@@ -62,6 +62,5 @@ namespace notifications
void show_toast(std::wstring plaintext_message, std::wstring title, toast_params params = {}); 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 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 update_toast_progress_bar(std::wstring_view tag, progress_bar_params params);
void update_toast_contents(std::wstring_view tag, std::wstring plaintext_message, std::wstring title);
void remove_toasts(std::wstring_view tag); void remove_toasts(std::wstring_view tag);
} }