Skip to content
This repository has been archived by the owner on Sep 4, 2020. It is now read-only.

Commit

Permalink
Issue #346: Is there any way to clear notifications out from the app?
Browse files Browse the repository at this point in the history
  • Loading branch information
macdonst committed May 27, 2016
1 parent 8c04a25 commit a69d007
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
22 changes: 22 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [push.setApplicationIconBadgeNumber()](#pushsetapplicationiconbadgenumbersuccesshandler-errorhandler-count---ios-only)
- [push.getApplicationIconBadgeNumber()](#pushgetapplicationiconbadgenumbersuccesshandler-errorhandler---ios-only)
- [push.finish()](#pushfinishsuccesshandler-errorhandler-id---ios-only)
- [push.clearAllNotifications()](#pushclearallnotificationssuccesshandler-errorhandler---ios-android-only)

## PushNotification.init(options)

Expand Down Expand Up @@ -338,3 +339,24 @@ push.finish(function() {
console.log('error');
}, 'push-1');
```

## push.clearAllNotifications(successHandler, errorHandler) - iOS, Android only

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.

### Example

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

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

describe('PushNotification instance', function () {
Expand Down
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 @@ -55,4 +55,5 @@ public interface PushConstants {
public static final String CONTENT_AVAILABLE = "content-available";
public static final String TOPICS = "topics";
public static final String SET_APPLICATION_ICON_BADGE_NUMBER = "setApplicationIconBadgeNumber";
public static final String CLEAR_ALL_NOTIFICATIONS = "clearAllNotifications";
}
16 changes: 14 additions & 2 deletions src/android/com/adobe/phonegap/push/PushPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ public void run() {
callbackContext.success();
}
});
} else if (CLEAR_ALL_NOTIFICATIONS.equals(action)) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
Log.v(LOG_TAG, "clearAllNotifications");
clearAllNotifications();
callbackContext.success();
}
});
} else {
Log.e(LOG_TAG, "Invalid action : " + action);
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.INVALID_ACTION));
Expand Down Expand Up @@ -254,8 +262,7 @@ public void onPause(boolean multitasking) {

SharedPreferences prefs = getApplicationContext().getSharedPreferences(COM_ADOBE_PHONEGAP_PUSH, Context.MODE_PRIVATE);
if (prefs.getBoolean(CLEAR_NOTIFICATIONS, true)) {
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
clearAllNotifications();
}
}

Expand All @@ -272,6 +279,11 @@ public void onDestroy() {
gWebView = null;
}

private void clearAllNotifications() {
final NotificationManager notificationManager = (NotificationManager) cordova.getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}

private void subscribeToTopics(JSONArray topics, String registrationToken) {
if (topics != null) {
String topic = null;
Expand Down
9 changes: 9 additions & 0 deletions src/ios/PushPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,15 @@ - (void)getApplicationIconBadgeNumber:(CDVInvokedUrlCommand *)command
[self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
}

- (void)clearAllNotifications:(CDVInvokedUrlCommand *)command
{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

NSString* message = [NSString stringWithFormat:@"cleared all notifications"];
CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message];
[self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
}

- (void)hasPermission:(CDVInvokedUrlCommand *)command
{
BOOL enabled = NO;
Expand Down
20 changes: 20 additions & 0 deletions www/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,26 @@ PushNotification.prototype.getApplicationIconBadgeNumber = function(successCallb
exec(successCallback, errorCallback, 'PushNotification', 'getApplicationIconBadgeNumber', []);
};

/**
* Get the application icon badge
*/

PushNotification.prototype.clearAllNotifications = function(successCallback, errorCallback) {
if (!errorCallback) { errorCallback = function() {}; }

if (typeof errorCallback !== 'function') {
console.log('PushNotification.clearAllNotifications failure: failure parameter not a function');
return;
}

if (typeof successCallback !== 'function') {
console.log('PushNotification.clearAllNotifications failure: success callback parameter must be a function');
return;
}

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

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

0 comments on commit a69d007

Please # to comment.