Skip to content

Commit d3c72cc

Browse files
authored
feat: add removeAllPendingNotificationRequests method (#206)
1 parent f41002a commit d3c72cc

File tree

5 files changed

+56
-21
lines changed

5 files changed

+56
-21
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -244,13 +244,13 @@ details is an object containing:
244244

245245
---
246246

247-
### `cancelAllLocalNotifications()`
247+
### `remooveAllPendingNotificationRequests()`
248248

249249
```jsx
250-
PushNotificationIOS.cancelAllLocalNotifications();
250+
PushNotificationIOS.remooveAllPendingNotificationRequests();
251251
```
252252

253-
Cancels all scheduled localNotifications
253+
Removes all pending notification requests in the notification center.
254254

255255
---
256256

example/App.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,11 @@ export const App = () => {
5555

5656
return () => {
5757
PushNotificationIOS.removeEventListener('register');
58-
PushNotificationIOS.removeEventListener(
59-
'registrationError'
60-
);
61-
PushNotificationIOS.removeEventListener(
62-
'notification'
63-
);
64-
PushNotificationIOS.removeEventListener(
65-
'localNotification'
66-
);
58+
PushNotificationIOS.removeEventListener('registrationError');
59+
PushNotificationIOS.removeEventListener('notification');
60+
PushNotificationIOS.removeEventListener('localNotification');
6761
};
62+
// eslint-disable-next-line react-hooks/exhaustive-deps
6863
}, []);
6964

7065
const sendNotification = () => {
@@ -87,7 +82,7 @@ export const App = () => {
8782
aps: {
8883
category: 'REACT_NATIVE',
8984
'content-available': 1,
90-
}
85+
},
9186
});
9287
};
9388

@@ -102,10 +97,14 @@ export const App = () => {
10297
const scheduleLocalNotification = () => {
10398
PushNotificationIOS.scheduleLocalNotification({
10499
alertBody: 'Test Local Notification',
105-
fireDate: new Date().toISOString(),
100+
fireDate: new Date(new Date().valueOf() + 2000).toISOString(),
106101
});
107102
};
108103

104+
const removeAllPendingNotificationRequests = () => {
105+
PushNotificationIOS.removeAllPendingNotificationRequests();
106+
};
107+
109108
const onRegistered = (deviceToken) => {
110109
Alert.alert('Registered For Remote Push', `Device Token: ${deviceToken}`, [
111110
{
@@ -129,7 +128,7 @@ export const App = () => {
129128
};
130129

131130
const onRemoteNotification = (notification) => {
132-
const isClicked = notification.getData().userInteraction === 1
131+
const isClicked = notification.getData().userInteraction === 1;
133132

134133
const result = `
135134
Title: ${notification.getTitle()};\n
@@ -148,7 +147,7 @@ export const App = () => {
148147
},
149148
]);
150149
} else {
151-
Alert.alert('Push Notification Received', result, [
150+
Alert.alert('Push Notification Received', result, [
152151
{
153152
text: 'Dismiss',
154153
onPress: null,
@@ -158,7 +157,7 @@ export const App = () => {
158157
};
159158

160159
const onLocalNotification = (notification) => {
161-
const isClicked = notification.getData().userInteraction === 1
160+
const isClicked = notification.getData().userInteraction === 1;
162161

163162
Alert.alert(
164163
'Local Notification Received',
@@ -192,8 +191,14 @@ export const App = () => {
192191
onPress={scheduleLocalNotification}
193192
label="Schedule fake local notification"
194193
/>
195-
196-
<Button onPress={sendSilentNotification} label="Send fake silent notification" />
194+
<Button
195+
onPress={removeAllPendingNotificationRequests}
196+
label="Remove All Pending Notification Requests"
197+
/>
198+
<Button
199+
onPress={sendSilentNotification}
200+
label="Send fake silent notification"
201+
/>
197202

198203
<Button
199204
onPress={() => PushNotificationIOS.setApplicationIconBadgeNumber(42)}

index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,16 @@ export interface PushNotificationIOSStatic {
210210

211211
/**
212212
* Cancels all scheduled localNotifications
213+
* @deprecated use `removeAllPendingNotificationRequests` instead
214+
* - This method is deprecated in iOS 10 and will be removed from future release
213215
*/
214216
cancelAllLocalNotifications(): void;
215217

218+
/**
219+
* Removes all pending notifications
220+
*/
221+
removeAllPendingNotificationRequests(): void;
222+
216223
/**
217224
* Remove all delivered notifications from Notification Center.
218225
*

ios/RNCPushNotificationIOS.m

+12
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,23 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
391391
[RCTSharedApplication() scheduleLocalNotification:notification];
392392
}
393393

394+
/**
395+
* Method not Available in iOS11+
396+
* TODO: This method will be removed in the next major version
397+
*/
394398
RCT_EXPORT_METHOD(cancelAllLocalNotifications)
395399
{
396400
[RCTSharedApplication() cancelAllLocalNotifications];
397401
}
398402

403+
RCT_EXPORT_METHOD(removeAllPendingNotificationRequests)
404+
{
405+
if ([UNUserNotificationCenter class]) {
406+
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
407+
[center removeAllPendingNotificationRequests];
408+
}
409+
}
410+
399411
RCT_EXPORT_METHOD(cancelLocalNotifications:(NSDictionary<NSString *, id> *)userInfo)
400412
{
401413
for (UILocalNotification *notification in RCTSharedApplication().scheduledLocalNotifications) {

js/index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ class PushNotificationIOS {
120120

121121
/**
122122
* Cancels all scheduled localNotifications.
123-
*
124-
* See https://reactnative.dev/docs/pushnotificationios.html#cancelalllocalnotifications
123+
* @deprecated use `removeAllPendingNotificationRequests` instead
124+
* - This method is deprecated in iOS 10 and will be removed from future release
125125
*/
126126
static cancelAllLocalNotifications() {
127127
invariant(
@@ -131,6 +131,17 @@ class PushNotificationIOS {
131131
RNCPushNotificationIOS.cancelAllLocalNotifications();
132132
}
133133

134+
/**
135+
* Removes all pending notifications
136+
*/
137+
static removeAllPendingNotificationRequests() {
138+
invariant(
139+
RNCPushNotificationIOS,
140+
'PushNotificationManager is not available.',
141+
);
142+
RNCPushNotificationIOS.removeAllPendingNotificationRequests();
143+
}
144+
134145
/**
135146
* Remove all delivered notifications from Notification Center.
136147
*

0 commit comments

Comments
 (0)