Skip to content

Commit

Permalink
fix(push-notifications): use id and tag for canceling active notifica…
Browse files Browse the repository at this point in the history
…tion (#1041)
  • Loading branch information
theproducer committed Jun 16, 2022
1 parent 7aa1d3a commit fa710a6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions push-notifications/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ Remove all native listeners for this plugin.
| **`subtitle`** | <code>string</code> | The notification subtitle. | 1.0.0 |
| **`body`** | <code>string</code> | The main text payload for the notification. | 1.0.0 |
| **`id`** | <code>string</code> | The notification identifier. | 1.0.0 |
| **`tag`** | <code>string</code> | The notification tag. Only available on Android (from push notifications). | 4.0.0 |
| **`badge`** | <code>number</code> | The number to display for the app icon badge. | 1.0.0 |
| **`notification`** | <code>any</code> | It's not being returned. | 1.0.0 |
| **`data`** | <code>any</code> | Any additional data that was included in the push notification payload. | 1.0.0 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -128,13 +129,18 @@ public void getDeliveredNotifications(PluginCall call) {
public void removeDeliveredNotifications(PluginCall call) {
JSArray notifications = call.getArray("notifications");

List<Integer> 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");
}
Expand All @@ -143,10 +149,6 @@ public void removeDeliveredNotifications(PluginCall call) {
call.reject(e.getMessage());
}

for (int id : ids) {
notificationManager.cancel(id);
}

call.resolve();
}

Expand Down
9 changes: 9 additions & 0 deletions push-notifications/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit fa710a6

Please sign in to comment.