Skip to content

Commit 1bad362

Browse files
authored
feat: add removePendingNotificationRequests method (#242)
1 parent 1506ddf commit 1bad362

File tree

5 files changed

+92
-5
lines changed

5 files changed

+92
-5
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,22 @@ Allows you to add specific actions for notification with specific category.
354354

355355
---
356356

357+
### `removePendingNotificationRequests()`
358+
359+
```jsx
360+
PushNotificationIOS.removeDeliveredNotifications(identifiers);
361+
```
362+
363+
Removes the specified pending notifications from Notification Center
364+
365+
**Parameters:**
366+
367+
| Name | Type | Required | Description |
368+
| ----------- | ----- | -------- | ---------------------------------- |
369+
| identifiers | string[] | Yes | Array of notification identifiers. |
370+
371+
---
372+
357373
### `removeAllPendingNotificationRequests()`
358374

359375
```jsx
@@ -405,13 +421,13 @@ A delivered notification is an object containing:
405421
PushNotificationIOS.removeDeliveredNotifications(identifiers);
406422
```
407423

408-
Removes the specified notifications from Notification Center
424+
Removes the specified delivered notifications from Notification Center
409425

410426
**Parameters:**
411427

412428
| Name | Type | Required | Description |
413429
| ----------- | ----- | -------- | ---------------------------------- |
414-
| identifiers | array | Yes | Array of notification identifiers. |
430+
| identifiers | string[] | Yes | Array of notification identifiers. |
415431

416432
---
417433

example/App.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,41 @@ export const App = (): React.Node => {
132132
});
133133
};
134134

135+
const addMultipleRequests = () => {
136+
PushNotificationIOS.addNotificationRequest({
137+
id: 'test-1',
138+
title: 'First',
139+
subtitle: 'subtitle',
140+
body: 'First Notification out of 3',
141+
category: 'test',
142+
threadId: 'thread-id',
143+
fireDate: new Date(new Date().valueOf() + 10000),
144+
repeats: true,
145+
});
146+
147+
PushNotificationIOS.addNotificationRequest({
148+
id: 'test-2',
149+
title: 'Second',
150+
subtitle: 'subtitle',
151+
body: 'Second Notification out of 3',
152+
category: 'test',
153+
threadId: 'thread-id',
154+
fireDate: new Date(new Date().valueOf() + 12000),
155+
repeats: true,
156+
});
157+
158+
PushNotificationIOS.addNotificationRequest({
159+
id: 'test-3',
160+
title: 'Third',
161+
subtitle: 'subtitle',
162+
body: 'Third Notification out of 3',
163+
category: 'test',
164+
threadId: 'thread-id',
165+
fireDate: new Date(new Date().valueOf() + 14000),
166+
repeats: true,
167+
});
168+
};
169+
135170
const getPendingNotificationRequests = () => {
136171
PushNotificationIOS.getPendingNotificationRequests((requests) => {
137172
Alert.alert('Push Notification Received', JSON.stringify(requests), [
@@ -179,6 +214,10 @@ export const App = (): React.Node => {
179214
PushNotificationIOS.removeAllPendingNotificationRequests();
180215
};
181216

217+
const removePendingNotificationRequests = () => {
218+
PushNotificationIOS.removePendingNotificationRequests(['test-1', 'test-2']);
219+
};
220+
182221
const onRegistered = (deviceToken) => {
183222
Alert.alert('Registered For Remote Push', `Device Token: ${deviceToken}`, [
184223
{
@@ -279,10 +318,18 @@ export const App = (): React.Node => {
279318
onPress={addNotificationRequest}
280319
label="Add Notification Request"
281320
/>
321+
<Button
322+
onPress={addMultipleRequests}
323+
label="Add Multiple Notification Requests"
324+
/>
282325
<Button
283326
onPress={setNotificationCategories}
284327
label="Set notification categories"
285328
/>
329+
<Button
330+
onPress={removePendingNotificationRequests}
331+
label="Remove Partial Pending Notification Requests"
332+
/>
286333
<Button
287334
onPress={removeAllPendingNotificationRequests}
288335
label="Remove All Pending Notification Requests"

index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,11 @@ export interface PushNotificationIOSStatic {
361361
*/
362362
removeAllPendingNotificationRequests(): void;
363363

364+
/**
365+
* Removes specified pending notifications from Notification Center.
366+
*/
367+
removePendingNotificationRequests(identifiers: string[]): void;
368+
364369
/**
365370
* Remove all delivered notifications from Notification Center.
366371
*

ios/RNCPushNotificationIOS.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,16 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
319319
RCT_EXPORT_METHOD(removeAllPendingNotificationRequests)
320320
{
321321
if ([UNUserNotificationCenter class]) {
322-
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
323-
[center removeAllPendingNotificationRequests];
322+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
323+
[center removeAllPendingNotificationRequests];
324+
}
325+
}
326+
327+
RCT_EXPORT_METHOD(removePendingNotificationRequests:(NSArray<NSString *> *)identifiers)
328+
{
329+
if ([UNUserNotificationCenter class]) {
330+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
331+
[center removePendingNotificationRequestsWithIdentifiers:identifiers];
324332
}
325333
}
326334

js/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,17 @@ class PushNotificationIOS {
182182
RNCPushNotificationIOS.removeAllPendingNotificationRequests();
183183
}
184184

185+
/**
186+
* Removes pending notifications with given identifier strings.
187+
*/
188+
static removePendingNotificationRequests(identifiers: string[]) {
189+
invariant(
190+
RNCPushNotificationIOS,
191+
'PushNotificationManager is not available.',
192+
);
193+
RNCPushNotificationIOS.removePendingNotificationRequests(identifiers);
194+
}
195+
185196
/**
186197
* Remove all delivered notifications from Notification Center.
187198
*
@@ -251,7 +262,7 @@ class PushNotificationIOS {
251262

252263
/**
253264
* Cancel local notifications.
254-
*
265+
* @deprecated - use `removePendingNotifications`
255266
* See https://reactnative.dev/docs/pushnotificationios.html#cancellocalnotification
256267
*/
257268
static cancelLocalNotifications(userInfo: Object) {

0 commit comments

Comments
 (0)