From fa710a63ea87f0ec0a7b0059baacfad7a45f8558 Mon Sep 17 00:00:00 2001 From: Joey Pender Date: Thu, 16 Jun 2022 11:30:24 -0500 Subject: [PATCH] fix(push-notifications): use id and tag for canceling active notification (#1041) --- push-notifications/README.md | 1 + .../pushnotifications/PushNotificationsPlugin.java | 14 ++++++++------ push-notifications/src/definitions.ts | 9 +++++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/push-notifications/README.md b/push-notifications/README.md index 04dae64b3..d68a68ca0 100644 --- a/push-notifications/README.md +++ b/push-notifications/README.md @@ -452,6 +452,7 @@ Remove all native listeners for this plugin. | **`subtitle`** | string | The notification subtitle. | 1.0.0 | | **`body`** | string | The main text payload for the notification. | 1.0.0 | | **`id`** | string | The notification identifier. | 1.0.0 | +| **`tag`** | string | The notification tag. Only available on Android (from push notifications). | 4.0.0 | | **`badge`** | number | The number to display for the app icon badge. | 1.0.0 | | **`notification`** | any | It's not being returned. | 1.0.0 | | **`data`** | any | Any additional data that was included in the push notification payload. | 1.0.0 | diff --git a/push-notifications/android/src/main/java/com/capacitorjs/plugins/pushnotifications/PushNotificationsPlugin.java b/push-notifications/android/src/main/java/com/capacitorjs/plugins/pushnotifications/PushNotificationsPlugin.java index 0cee22647..f72643c00 100644 --- a/push-notifications/android/src/main/java/com/capacitorjs/plugins/pushnotifications/PushNotificationsPlugin.java +++ b/push-notifications/android/src/main/java/com/capacitorjs/plugins/pushnotifications/PushNotificationsPlugin.java @@ -98,6 +98,7 @@ public void getDeliveredNotifications(PluginCall call) { JSObject jsNotif = new JSObject(); jsNotif.put("id", notif.getId()); + jsNotif.put("tag", notif.getTag()); Notification notification = notif.getNotification(); if (notification != null) { @@ -128,13 +129,18 @@ public void getDeliveredNotifications(PluginCall call) { public void removeDeliveredNotifications(PluginCall call) { JSArray notifications = call.getArray("notifications"); - List ids = new ArrayList<>(); try { for (Object o : notifications.toList()) { if (o instanceof JSONObject) { JSObject notif = JSObject.fromJSONObject((JSONObject) o); + String tag = notif.getString("tag"); Integer id = notif.getInteger("id"); - ids.add(id); + + if (tag == null) { + notificationManager.cancel(id); + } else { + notificationManager.cancel(tag, id); + } } else { call.reject("Expected notifications to be a list of notification objects"); } @@ -143,10 +149,6 @@ public void removeDeliveredNotifications(PluginCall call) { call.reject(e.getMessage()); } - for (int id : ids) { - notificationManager.cancel(id); - } - call.resolve(); } diff --git a/push-notifications/src/definitions.ts b/push-notifications/src/definitions.ts index 4d34c1ce5..3a3a8f33e 100644 --- a/push-notifications/src/definitions.ts +++ b/push-notifications/src/definitions.ts @@ -197,6 +197,15 @@ export interface PushNotificationSchema { */ id: string; + /** + * The notification tag. + * + * Only available on Android (from push notifications). + * + * @since 4.0.0 + */ + tag?: string; + /** * The number to display for the app icon badge. *