Skip to content

Commit

Permalink
feat: add removePendingNotificationRequests method (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
Naturalclar authored Nov 25, 2020
1 parent 1506ddf commit 1bad362
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,22 @@ Allows you to add specific actions for notification with specific category.

---

### `removePendingNotificationRequests()`

```jsx
PushNotificationIOS.removeDeliveredNotifications(identifiers);
```

Removes the specified pending notifications from Notification Center

**Parameters:**

| Name | Type | Required | Description |
| ----------- | ----- | -------- | ---------------------------------- |
| identifiers | string[] | Yes | Array of notification identifiers. |

---

### `removeAllPendingNotificationRequests()`

```jsx
Expand Down Expand Up @@ -405,13 +421,13 @@ A delivered notification is an object containing:
PushNotificationIOS.removeDeliveredNotifications(identifiers);
```

Removes the specified notifications from Notification Center
Removes the specified delivered notifications from Notification Center

**Parameters:**

| Name | Type | Required | Description |
| ----------- | ----- | -------- | ---------------------------------- |
| identifiers | array | Yes | Array of notification identifiers. |
| identifiers | string[] | Yes | Array of notification identifiers. |

---

Expand Down
47 changes: 47 additions & 0 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,41 @@ export const App = (): React.Node => {
});
};

const addMultipleRequests = () => {
PushNotificationIOS.addNotificationRequest({
id: 'test-1',
title: 'First',
subtitle: 'subtitle',
body: 'First Notification out of 3',
category: 'test',
threadId: 'thread-id',
fireDate: new Date(new Date().valueOf() + 10000),
repeats: true,
});

PushNotificationIOS.addNotificationRequest({
id: 'test-2',
title: 'Second',
subtitle: 'subtitle',
body: 'Second Notification out of 3',
category: 'test',
threadId: 'thread-id',
fireDate: new Date(new Date().valueOf() + 12000),
repeats: true,
});

PushNotificationIOS.addNotificationRequest({
id: 'test-3',
title: 'Third',
subtitle: 'subtitle',
body: 'Third Notification out of 3',
category: 'test',
threadId: 'thread-id',
fireDate: new Date(new Date().valueOf() + 14000),
repeats: true,
});
};

const getPendingNotificationRequests = () => {
PushNotificationIOS.getPendingNotificationRequests((requests) => {
Alert.alert('Push Notification Received', JSON.stringify(requests), [
Expand Down Expand Up @@ -179,6 +214,10 @@ export const App = (): React.Node => {
PushNotificationIOS.removeAllPendingNotificationRequests();
};

const removePendingNotificationRequests = () => {
PushNotificationIOS.removePendingNotificationRequests(['test-1', 'test-2']);
};

const onRegistered = (deviceToken) => {
Alert.alert('Registered For Remote Push', `Device Token: ${deviceToken}`, [
{
Expand Down Expand Up @@ -279,10 +318,18 @@ export const App = (): React.Node => {
onPress={addNotificationRequest}
label="Add Notification Request"
/>
<Button
onPress={addMultipleRequests}
label="Add Multiple Notification Requests"
/>
<Button
onPress={setNotificationCategories}
label="Set notification categories"
/>
<Button
onPress={removePendingNotificationRequests}
label="Remove Partial Pending Notification Requests"
/>
<Button
onPress={removeAllPendingNotificationRequests}
label="Remove All Pending Notification Requests"
Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,11 @@ export interface PushNotificationIOSStatic {
*/
removeAllPendingNotificationRequests(): void;

/**
* Removes specified pending notifications from Notification Center.
*/
removePendingNotificationRequests(identifiers: string[]): void;

/**
* Remove all delivered notifications from Notification Center.
*
Expand Down
12 changes: 10 additions & 2 deletions ios/RNCPushNotificationIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,16 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
RCT_EXPORT_METHOD(removeAllPendingNotificationRequests)
{
if ([UNUserNotificationCenter class]) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removeAllPendingNotificationRequests];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removeAllPendingNotificationRequests];
}
}

RCT_EXPORT_METHOD(removePendingNotificationRequests:(NSArray<NSString *> *)identifiers)
{
if ([UNUserNotificationCenter class]) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removePendingNotificationRequestsWithIdentifiers:identifiers];
}
}

Expand Down
13 changes: 12 additions & 1 deletion js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ class PushNotificationIOS {
RNCPushNotificationIOS.removeAllPendingNotificationRequests();
}

/**
* Removes pending notifications with given identifier strings.
*/
static removePendingNotificationRequests(identifiers: string[]) {
invariant(
RNCPushNotificationIOS,
'PushNotificationManager is not available.',
);
RNCPushNotificationIOS.removePendingNotificationRequests(identifiers);
}

/**
* Remove all delivered notifications from Notification Center.
*
Expand Down Expand Up @@ -251,7 +262,7 @@ class PushNotificationIOS {

/**
* Cancel local notifications.
*
* @deprecated - use `removePendingNotifications`
* See https://reactnative.dev/docs/pushnotificationios.html#cancellocalnotification
*/
static cancelLocalNotifications(userInfo: Object) {
Expand Down

0 comments on commit 1bad362

Please sign in to comment.