Skip to content

feat: add removePendingNotificationRequests method #242

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

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()`
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi,
There is a small typo between the title and the exemple in code.
removePendingNotificationRequests vs removeDeliveredNotifications
😉


```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