diff --git a/docs/API.md b/docs/API.md index 0807e73f5..178125c82 100644 --- a/docs/API.md +++ b/docs/API.md @@ -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) @@ -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 @@ -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); +``` \ No newline at end of file diff --git a/spec/index.spec.js b/spec/index.spec.js index 9333d4415..a7123e0af 100644 --- a/spec/index.spec.js +++ b/spec/index.spec.js @@ -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({}); @@ -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); + }); + }); + }); }); }); diff --git a/src/android/com/adobe/phonegap/push/PushConstants.java b/src/android/com/adobe/phonegap/push/PushConstants.java index 02f885e93..9b4656a00 100644 --- a/src/android/com/adobe/phonegap/push/PushConstants.java +++ b/src/android/com/adobe/phonegap/push/PushConstants.java @@ -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"; } diff --git a/src/android/com/adobe/phonegap/push/PushPlugin.java b/src/android/com/adobe/phonegap/push/PushPlugin.java index dc81f1c5b..121bd5a25 100644 --- a/src/android/com/adobe/phonegap/push/PushPlugin.java +++ b/src/android/com/adobe/phonegap/push/PushPlugin.java @@ -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)); @@ -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; diff --git a/src/js/push.js b/src/js/push.js index b05df8493..f952cacbf 100644 --- a/src/js/push.js +++ b/src/js/push.js @@ -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. * diff --git a/www/push.js b/www/push.js index 3bd51b5c4..7aea0c648 100644 --- a/www/push.js +++ b/www/push.js @@ -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. *