diff --git a/.changes/notification-cancel-all-android.md b/.changes/notification-cancel-all-android.md new file mode 100644 index 0000000000..fee83deb1b --- /dev/null +++ b/.changes/notification-cancel-all-android.md @@ -0,0 +1,6 @@ +--- +"notification": patch +"notification-js": patch +--- + +Fix `cancel` command crashing on Android with `UninitializedPropertyAccessException` when invoked with no `notifications` argument — i.e. when the JS `cancelAll()` API or the Rust `NotificationExt::cancel_all()` SDK method is used. `CancelArgs.notifications` is now an optional list defaulting to `[]`; the handler routes an empty list to a new `TauriNotificationManager.cancelAll()` that enumerates saved notification IDs from storage and cancels each. diff --git a/plugins/notification/android/src/main/java/NotificationPlugin.kt b/plugins/notification/android/src/main/java/NotificationPlugin.kt index 3ead31527b..07c545d795 100644 --- a/plugins/notification/android/src/main/java/NotificationPlugin.kt +++ b/plugins/notification/android/src/main/java/NotificationPlugin.kt @@ -39,7 +39,7 @@ class BatchArgs { @InvokeArg class CancelArgs { - lateinit var notifications: List + var notifications: List = listOf() } @InvokeArg @@ -152,7 +152,11 @@ class NotificationPlugin(private val activity: Activity): Plugin(activity) { @Command fun cancel(invoke: Invoke) { val args = invoke.parseArgs(CancelArgs::class.java) - manager.cancel(args.notifications) + if (args.notifications.isEmpty()) { + manager.cancelAll() + } else { + manager.cancel(args.notifications) + } invoke.resolve() } diff --git a/plugins/notification/android/src/main/java/TauriNotificationManager.kt b/plugins/notification/android/src/main/java/TauriNotificationManager.kt index a8912739bb..401c0d1db7 100644 --- a/plugins/notification/android/src/main/java/TauriNotificationManager.kt +++ b/plugins/notification/android/src/main/java/TauriNotificationManager.kt @@ -391,6 +391,10 @@ class TauriNotificationManager( } } + fun cancelAll() { + cancel(storage.getSavedNotificationIds().mapNotNull { it.toIntOrNull() }) + } + private fun cancelTimerForNotification(notificationId: Int) { val intent = Intent(context, TimedNotificationPublisher::class.java) var flags = 0