Skip to content

Commit

Permalink
✨ Added a clearNotification method to clear a single notification (An…
Browse files Browse the repository at this point in the history
…droid Only) (phonegap#2192)

* Added a clearNotification method to clear a single notification

(Android only)

* Added changes to docs, updated formatting, added tests

* Updated API.md, Moved id arg, added more error checking
  • Loading branch information
DavidBriglio authored and macdonst committed Jul 3, 2018
1 parent 87ded60 commit 683696e
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 21 deletions.
65 changes: 44 additions & 21 deletions docs/API.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
# API

* [.init()](#pushnotificationinitoptions)
* [.hasPermission()](#pushnotificationhaspermissionsuccesshandler)
* [.createChannel() - Android only](#pushnotificationcreatechannelsuccesshandler-failurehandler-channel)
* [.deleteChannel() - Android only](#pushnotificationdeletechannelsuccesshandler-failurehandler-channelid)
* [.listChannels() - Android only](#pushnotificationlistchannelssuccesshandler)
* [push.on()](#pushonevent-callback)
* [push.on('registration')](#pushonregistration-callback)
* [push.on('notification')](#pushonnotification-callback)
* [push.on('error')](#pushonerror-callback)
* [push.off()](#pushoffevent-callback)
* [push.unregister()](#pushunregistersuccesshandler-errorhandler-topics)
* [push.subscribe()](#pushsubscribetopic-successhandler-errorhandler)
* [push.unsubscribe()](#pushunsubscribetopic-successhandler-errorhandler)
* [push.setApplicationIconBadgeNumber() - iOS & Android only](#pushsetapplicationiconbadgenumbersuccesshandler-errorhandler-count---ios--android-only)
* [push.getApplicationIconBadgeNumber() - iOS & Android only](#pushgetapplicationiconbadgenumbersuccesshandler-errorhandler---ios--android-only)
* [push.finish() - iOS only](#pushfinishsuccesshandler-errorhandler-id---ios-only)
* [push.clearAllNotifications() - iOS & Android only](#pushclearallnotificationssuccesshandler-errorhandler---ios--android-only)
- [.init()](#pushnotificationinitoptions)
- [.hasPermission()](#pushnotificationhaspermissionsuccesshandler)
- [.createChannel() - Android only](#pushnotificationcreatechannel)
- [.deleteChannel() - Android only](#pushnotificationdeletechannel)
- [.listChannels() - Android only](#pushnotificationlistchannels)
- [push.on()](#pushonevent-callback)
- [push.on('registration')](#pushonregistration-callback)
- [push.on('notification')](#pushonnotification-callback)
- [push.on('error')](#pushonerror-callback)
- [push.off()](#pushoffevent-callback)
- [push.unregister()](#pushunregistersuccesshandler-errorhandler-topics)
- [push.subscribe()](#pushsubscribetopic-successhandler-errorhandler)
- [push.unsubscribe()](#pushunsubscribetopic-successhandler-errorhandler)
- [push.setApplicationIconBadgeNumber() - iOS & Android only](#pushsetapplicationiconbadgenumbersuccesshandler-errorhandler-count---ios--android-only)
- [push.getApplicationIconBadgeNumber() - iOS & Android only](#pushgetapplicationiconbadgenumbersuccesshandler-errorhandler---ios--android-only)
- [push.finish() - iOS only](#pushfinishsuccesshandler-errorhandler-id---ios-only)
- [push.clearAllNotifications() - iOS & Android only](#pushclearallnotificationssuccesshandler-errorhandler---ios--android-only)
- [push.clearNotification() - Android only](#pushclearnotificationid-successhandler-errorhandler---android-only)

## PushNotification.init(options)

Expand Down Expand Up @@ -573,10 +574,10 @@ Tells the OS to clear all notifications from the Notification Center

### Parameters

| Parameter | Type | Default | Description |
| ---------------- | ---------- | ------- | --------------------------------------------------------------------------------------- |
| `successHandler` | `Function` | | Is called when the api successfully clears the notifications. |
| `errorHandler` | `Function` | | Is called when the api encounters an error when attempting to clears the notifications. |
Parameter | Type | Default | Description
--------- | ---- | ------- | -----------
`successHandler` | `Function` | | Is called when the api successfully clears the notifications.
`errorHandler` | `Function` | | Is called when the api encounters an error when attempting to clear the notifications.

### Example

Expand All @@ -590,3 +591,25 @@ push.clearAllNotifications(
}
);
```

## push.clearNotification(id, successHandler, errorHandler) - Android only

Tells the OS to clear the notification that corresponds to the id argument, from the Notification Center

### Parameters

Parameter | Type | Default | Description
--------- | ---- | ------- | -----------
`successHandler` | `Function` | | Is called when the api successfully clears the notification.
`errorHandler` | `Function` | | Is called when the api encounters an error when attempting to clear the notification.
`id` | `number` | | The ID of the notification that will be cleared. |

### Example

```javascript
push.clearNotification(() => {
console.log('success');
}, () => {
console.log('error');
}, 145);
```
40 changes: 40 additions & 0 deletions spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ describe('phonegap-plugin-push', function () {
expect(push.clearAllNotifications).toBeDefined();
expect(typeof push.clearAllNotifications === 'function').toBe(true);
});

it('should contain a clearNotification function', function () {
var push = PushNotification.init({});
expect(push.clearNotification).toBeDefined();
expect(typeof push.clearNotification === 'function').toBe(true);
});

it('should contain a subscribe function', function () {
var push = PushNotification.init({});
Expand Down Expand Up @@ -423,5 +429,39 @@ describe('phonegap-plugin-push', function () {
});
});
});

describe('clear notification method', function () {
describe('cordova.exec', function () {
it('should call cordova.exec on next process tick using number argument', function (done) {
var push = PushNotification.init(options);
push.clearNotification(function () {}, function () {}, 145);
setTimeout(function () {
expect(execSpy).toHaveBeenCalledWith(
jasmine.any(Function),
jasmine.any(Function),
'PushNotification',
'clearNotification',
[145]
);
done();
}, 100);
});

it('should call cordova.exec on next process tick using string argument', function (done) {
var push = PushNotification.init(options);
push.clearNotification(function () {}, function () {}, "145");
setTimeout(function () {
expect(execSpy).toHaveBeenCalledWith(
jasmine.any(Function),
jasmine.any(Function),
'PushNotification',
'clearNotification',
[145]
);
done();
}, 100);
});
});
});
});
});
1 change: 1 addition & 0 deletions src/android/com/adobe/phonegap/push/PushConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@ public interface PushConstants {
public static final String DELETE_CHANNEL = "deleteChannel";
public static final String ONGOING = "ongoing";
public static final String LIST_CHANNELS = "listChannels";
public static final String CLEAR_NOTIFICATION = "clearNotification";
}
21 changes: 21 additions & 0 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,20 @@ public void run() {
}
}
});
} else if (CLEAR_NOTIFICATION.equals(action)) {
// clearing a single notification
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
Log.v(LOG_TAG, "clearNotification");
int id = data.getInt(0);
clearNotification(id);
callbackContext.success();
} catch (JSONException e) {
callbackContext.error(e.getMessage());
}
}
});
} else {
Log.e(LOG_TAG, "Invalid action : " + action);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
Expand Down Expand Up @@ -508,6 +522,13 @@ private void clearAllNotifications() {
notificationManager.cancelAll();
}

private void clearNotification(int id) {
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity()
.getSystemService(Context.NOTIFICATION_SERVICE);
String appName = (String) this.cordova.getActivity().getPackageManager().getApplicationLabel(this.cordova.getActivity().getApplicationInfo());
notificationManager.cancel(appName, id);
}

private void subscribeToTopics(JSONArray topics, String registrationToken) {
if (topics != null) {
String topic = null;
Expand Down
21 changes: 21 additions & 0 deletions src/js/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,27 @@ class PushNotification {

exec(successCallback, errorCallback, 'PushNotification', 'clearAllNotifications', []);
}

/**
* Clears notifications that have the ID specified.
* @param {Function} [successCallback] Callback function to be called on success.
* @param {Function} [errorCallback] Callback function to be called when an error is encountered.
* @param {Number} id ID of the notification to be removed.
*/
clearNotification(successCallback = () => {}, errorCallback = () => {}, id) {
const idNumber = parseInt(id, 10);
if (isNaN(idNumber) || idNumber > Number.MAX_SAFE_INTEGER || idNumber < 0) {
console.log(
'PushNotification.clearNotification failure: id parameter must' +
'be a valid integer.'
);
return;
}

exec(successCallback, errorCallback, 'PushNotification', 'clearNotification',
[idNumber]);
}

/**
* Listen for an event.
*
Expand Down
24 changes: 24 additions & 0 deletions www/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,30 @@ var PushNotification = function () {

exec(successCallback, errorCallback, 'PushNotification', 'clearAllNotifications', []);
}

/**
* Clears notifications that have the ID specified.
* @param {Function} [successCallback] Callback function to be called on success.
* @param {Function} [errorCallback] Callback function to be called when an error is encountered.
* @param {Number} id ID of the notification to be removed.
*/

}, {
key: 'clearNotification',
value: function clearNotification() {
var successCallback = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
var errorCallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
var id = arguments[2];

var idNumber = parseInt(id, 10);
if (isNaN(idNumber) || idNumber > Number.MAX_SAFE_INTEGER || idNumber < 0) {
console.log('PushNotification.clearNotification failure: id parameter must' + 'be a valid integer.');
return;
}

exec(successCallback, errorCallback, 'PushNotification', 'clearNotification', [idNumber]);
}

/**
* Listen for an event.
*
Expand Down

0 comments on commit 683696e

Please sign in to comment.