Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(push-notifications): use id and tag for canceling active notification #1041

Merged
merged 11 commits into from
Jun 16, 2022
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). | 1.0.9 |
| **`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 @@ -129,13 +129,18 @@ public void getDeliveredNotifications(PluginCall call) {
public void removeDeliveredNotifications(PluginCall call) {
JSArray notifications = call.getArray("notifications");

List<String> tags = new ArrayList<>();
try {
for (Object o : notifications.toList()) {
if (o instanceof JSONObject) {
JSObject notif = JSObject.fromJSONObject((JSONObject) o);
String tag = notif.getString("tag");
jcesarmobile marked this conversation as resolved.
Show resolved Hide resolved
tags.add(tag);
Integer id = notif.getInteger("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 @@ -144,10 +149,6 @@ public void removeDeliveredNotifications(PluginCall call) {
call.reject(e.getMessage());
}

for (String tag : tags) {
notificationManager.cancel(tag, 0);
}

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 1.0.9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 1.0.9
* @since 4.0.0

This will be available on version 4.0.0 of the plugin

*/
tag?: string;

/**
* The number to display for the app icon badge.
*
Expand Down