Skip to content
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

feat: add removeAllPendingNotificationRequests method #206

Merged
merged 5 commits into from
Oct 24, 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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ details is an object containing:

---

### `cancelAllLocalNotifications()`
### `remooveAllPendingNotificationRequests()`

```jsx
PushNotificationIOS.cancelAllLocalNotifications();
PushNotificationIOS.remooveAllPendingNotificationRequests();
```

Cancels all scheduled localNotifications
Removes all pending notification requests in the notification center.

---

Expand Down
37 changes: 21 additions & 16 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,11 @@ export const App = () => {

return () => {
PushNotificationIOS.removeEventListener('register');
PushNotificationIOS.removeEventListener(
'registrationError'
);
PushNotificationIOS.removeEventListener(
'notification'
);
PushNotificationIOS.removeEventListener(
'localNotification'
);
PushNotificationIOS.removeEventListener('registrationError');
PushNotificationIOS.removeEventListener('notification');
PushNotificationIOS.removeEventListener('localNotification');
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const sendNotification = () => {
Expand All @@ -87,7 +82,7 @@ export const App = () => {
aps: {
category: 'REACT_NATIVE',
'content-available': 1,
}
},
});
};

Expand All @@ -102,10 +97,14 @@ export const App = () => {
const scheduleLocalNotification = () => {
PushNotificationIOS.scheduleLocalNotification({
alertBody: 'Test Local Notification',
fireDate: new Date().toISOString(),
fireDate: new Date(new Date().valueOf() + 2000).toISOString(),
});
};

const removeAllPendingNotificationRequests = () => {
PushNotificationIOS.removeAllPendingNotificationRequests();
};

const onRegistered = (deviceToken) => {
Alert.alert('Registered For Remote Push', `Device Token: ${deviceToken}`, [
{
Expand All @@ -129,7 +128,7 @@ export const App = () => {
};

const onRemoteNotification = (notification) => {
const isClicked = notification.getData().userInteraction === 1
const isClicked = notification.getData().userInteraction === 1;

const result = `
Title: ${notification.getTitle()};\n
Expand All @@ -148,7 +147,7 @@ export const App = () => {
},
]);
} else {
Alert.alert('Push Notification Received', result, [
Alert.alert('Push Notification Received', result, [
{
text: 'Dismiss',
onPress: null,
Expand All @@ -158,7 +157,7 @@ export const App = () => {
};

const onLocalNotification = (notification) => {
const isClicked = notification.getData().userInteraction === 1
const isClicked = notification.getData().userInteraction === 1;

Alert.alert(
'Local Notification Received',
Expand Down Expand Up @@ -192,8 +191,14 @@ export const App = () => {
onPress={scheduleLocalNotification}
label="Schedule fake local notification"
/>

<Button onPress={sendSilentNotification} label="Send fake silent notification" />
<Button
onPress={removeAllPendingNotificationRequests}
label="Remove All Pending Notification Requests"
/>
<Button
onPress={sendSilentNotification}
label="Send fake silent notification"
/>

<Button
onPress={() => PushNotificationIOS.setApplicationIconBadgeNumber(42)}
Expand Down
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,16 @@ export interface PushNotificationIOSStatic {

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

/**
* Removes all pending notifications
*/
removeAllPendingNotificationRequests(): void;

/**
* Remove all delivered notifications from Notification Center.
*
Expand Down
12 changes: 12 additions & 0 deletions ios/RNCPushNotificationIOS.m
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,23 @@ - (void)handleRemoteNotificationRegistrationError:(NSNotification *)notification
[RCTSharedApplication() scheduleLocalNotification:notification];
}

/**
* Method not Available in iOS11+
* TODO: This method will be removed in the next major version
*/
RCT_EXPORT_METHOD(cancelAllLocalNotifications)
{
[RCTSharedApplication() cancelAllLocalNotifications];
}

RCT_EXPORT_METHOD(removeAllPendingNotificationRequests)
{
if ([UNUserNotificationCenter class]) {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removeAllPendingNotificationRequests];
}
}

RCT_EXPORT_METHOD(cancelLocalNotifications:(NSDictionary<NSString *, id> *)userInfo)
{
for (UILocalNotification *notification in RCTSharedApplication().scheduledLocalNotifications) {
Expand Down
15 changes: 13 additions & 2 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ class PushNotificationIOS {

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

/**
* Removes all pending notifications
*/
static removeAllPendingNotificationRequests() {
invariant(
RNCPushNotificationIOS,
'PushNotificationManager is not available.',
);
RNCPushNotificationIOS.removeAllPendingNotificationRequests();
}

/**
* Remove all delivered notifications from Notification Center.
*
Expand Down