Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class FlutterLocalNotificationsWindows extends WindowsNotificationsBase {
DidReceiveNotificationResponseCallback? userCallback;

/// Calls made while initializing share the first call's settings and result.
/// After a false result, call [initialize] again to retry or [dispose].
@override
Future<bool> initialize({
required WindowsInitializationSettings settings,
Expand Down
21 changes: 13 additions & 8 deletions flutter_local_notifications_windows/src/ffi_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ bool init(
} catch (...) {
// Reported as a failure below, so the caller never waits forever.
}
if (!plugin->isReady) plugin->resetRegistration();
try {
if (!plugin->isReady) plugin->resetRegistration();
} catch (...) {
// onReady must still run, or the caller waits forever.
}
onReady(plugin->isReady);
});
}
Expand All @@ -65,11 +69,14 @@ bool isValidXml(char* xml) {
}

// Collects matches first: removing while iterating the scheduled view is unsafe.
static void removeScheduled(NativePlugin* plugin, const winrt::hstring& tag) {
// A null [tag] removes every scheduled toast.
static void removeScheduled(NativePlugin* plugin, const winrt::hstring* tag) {
auto& notifier = plugin->notifier.value();
std::vector<ScheduledToastNotification> matches;
for (const auto notification : notifier.GetScheduledToastNotifications()) {
if (notification.Tag() == tag) matches.push_back(notification);
if (tag == nullptr || notification.Tag() == *tag) {
matches.push_back(notification);
}
}
for (const auto& notification : matches) notifier.RemoveFromSchedule(notification);
}
Expand Down Expand Up @@ -107,7 +114,7 @@ bool scheduleNotification(NativePlugin* plugin, int id, char* xml, int64_t time)
return;
}
const auto tag = winrt::to_hstring(id);
removeScheduled(plugin, tag);
removeScheduled(plugin, &tag);
const auto deliveryTime = winrt::clock::from_time_t(time);
// The job may run late behind a stalled call, and AddToSchedule throws for a past time.
if (deliveryTime <= winrt::clock::now()) {
Expand Down Expand Up @@ -142,9 +149,7 @@ void cancelAll(NativePlugin* plugin) {
} else {
plugin->history.value().Clear(plugin->aumid);
}
for (const auto notification : plugin->notifier.value().GetScheduledToastNotifications()) {
plugin->notifier.value().RemoveFromSchedule(notification);
}
removeScheduled(plugin, nullptr);
});
}

Expand All @@ -153,7 +158,7 @@ void cancelNotification(NativePlugin* plugin, int id) {
if (!plugin->isReady) return;
const auto tag = winrt::to_hstring(id);
if (plugin->hasIdentity) plugin->history.value().Remove(tag);
removeScheduled(plugin, tag);
removeScheduled(plugin, &tag);
});
}

Expand Down
Loading