Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Replace deprecated local notification methods on iOS #1751

Merged
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
11 changes: 10 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,13 @@ local.properties
example

# Git
.git
.git

# GitHub
.github/*

# Docs
submitting-a-pull-request.md

# Vscode
.vscode/*
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public class MainApplication extends Application implements ReactApplication {

```javascript
import PushNotificationIOS from "@react-native-community/push-notification-ios";
var PushNotification = require("react-native-push-notification");
import PushNotification from "react-native-push-notification";

// Must be outside of any component LifeCycle (such as `componentDidMount`).
PushNotification.configure({
Expand Down Expand Up @@ -336,7 +336,6 @@ PushNotification.localNotification({
invokeApp: true, // (optional) This enable click on actions to bring back the application to foreground or stay in background, default: true

/* iOS only properties */
alertAction: "view", // (optional) default: view
category: "", // (optional) default: empty string

/* iOS and Android properties */
Expand Down Expand Up @@ -497,20 +496,18 @@ PushNotification.cancelLocalNotifications({id: '123'});

Cancels all scheduled notifications AND clears the notifications alerts that are in the notification centre.

_NOTE: there is currently no api for removing specific notification alerts from the notification centre._

### 3) removeAllDeliveredNotifications

```javascript
PushNotificationIOS.removeAllDeliveredNotifications();
PushNotification.removeAllDeliveredNotifications();
```

Remove all delivered notifications from Notification Center

### 4) getDeliveredNotifications

```javascript
PushNotificationIOS.getDeliveredNotifications(callback);
PushNotification.getDeliveredNotifications(callback);
```

Provides you with a list of the app’s notifications that are still displayed in Notification Center
Expand All @@ -533,7 +530,7 @@ A delivered notification is an object containing:
### 5) removeDeliveredNotifications

```javascript
PushNotificationIOS.removeDeliveredNotifications(identifiers);
PushNotification.removeDeliveredNotifications(identifiers);
```

Removes the specified notifications from Notification Center
Expand All @@ -547,7 +544,7 @@ Removes the specified notifications from Notification Center
### 6) getScheduledLocalNotifications

```javascript
PushNotificationIOS.getScheduledLocalNotifications(callback);
PushNotification.getScheduledLocalNotifications(callback);
```

Provides you with a list of the app’s scheduled local notifications that are yet to be displayed
Expand All @@ -560,15 +557,15 @@ Provides you with a list of the app’s scheduled local notifications that are y

Returns an array of local scheduled notification objects containing:

| Name | Type | Description |
| -------------- | ------ | ----------------------------------------- |
| id | number | The identifier of this notification. |
| date | Date | The fire date of this notification. |
| title | string | The title of this notification. |
| message | string | The message body of this notification. |
| soundName | string | The sound name of this notification. |
| repeatInterval | number | The repeat interval of this notification. |
| number | number | App notification badge count number. |
| Name | Type | Description |
| -------------- | ------ | -------------------------------------------------------- |
| id | number | The identifier of this notification. |
| date | Date | The fire date of this notification. |
| title | string | The title of this notification. |
| message | string | The message body of this notification. |
| soundName | string | The sound name of this notification. |
| repeatInterval | number | (Android only) The repeat interval of this notification. |
| number | number | App notification badge count number. |

## Abandon Permissions

Expand Down Expand Up @@ -631,6 +628,10 @@ https://developer.android.com/training/monitoring-device-state/doze-standby

(optional) Specify `repeatType` and optionally `repeatTime` (Android-only) while scheduling the local notification. Check the local notification example above.

### iOS
Property `repeatType` can only be `day`.

### Android
Property `repeatType` could be one of `month`, `week`, `day`, `hour`, `minute`, `time`. If specified as time, it should be accompanied by one more parameter `repeatTime` which should the number of milliseconds between each interval.

## Notification Actions
Expand Down
2 changes: 0 additions & 2 deletions example/NotifService.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ export default class NotifService {
timeoutAfter: null, // (optional) Specifies a duration in milliseconds after which this notification should be canceled, if it is not already canceled, default: null

/* iOS only properties */
alertAction: 'view', // (optional) default: view
category: '', // (optional) default: empty string

/* iOS and Android properties */
Expand Down Expand Up @@ -135,7 +134,6 @@ export default class NotifService {
timeoutAfter: null, // (optional) Specifies a duration in milliseconds after which this notification should be canceled, if it is not already canceled, default: null

/* iOS only properties */
alertAction: 'view', // (optional) default: view
category: '', // (optional) default: empty string

/* iOS and Android properties */
Expand Down
64 changes: 35 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,14 @@ Notifications.localNotification = function(details) {
}

// for valid fields see: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html
// alertTitle only valid for apple watch: https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/#//apple_ref/occ/instp/UILocalNotification/alertTitle

this.handler.presentLocalNotification({
alertTitle: details.title,
alertBody: details.message,
alertAction: details.alertAction,
this.handler.addNotificationRequest({
id: (!details.id ? Math.floor(Math.random() * Math.pow(2, 32)).toString() : details.id),
title: details.title,
body: details.message,
badge: details.number,
sound: soundName,
category: details.category,
soundName: soundName,
applicationIconBadgeNumber: details.number,
userInfo: details.userInfo
});
} else {
Expand Down Expand Up @@ -250,25 +249,21 @@ Notifications.localNotificationSchedule = function(details) {
}

const iosDetails = {
id: (!details.id ? Math.floor(Math.random() * Math.pow(2, 32)).toString() : details.id),
fireDate: details.date.toISOString(),
alertTitle: details.title,
alertBody: details.message,
title: details.title,
body: details.message,
sound: soundName,
category: details.category,
soundName: soundName,
userInfo: details.userInfo,
repeatInterval: details.repeatType,
category: details.category,
repeats: (details.repeatType && details.repeatType == "day"),
};

if (details.number) {
iosDetails.applicationIconBadgeNumber = parseInt(details.number, 10);
iosDetails.badge = parseInt(details.number, 10);
}

// ignore Android only repeatType
if (!details.repeatType || details.repeatType === 'time') {
delete iosDetails.repeatInterval;
}
this.handler.scheduleLocalNotification(iosDetails);
this.handler.addNotificationRequest(iosDetails);
} else {
if (details && typeof details.number === 'number') {
if (isNaN(details.number)) {
Expand Down Expand Up @@ -456,16 +451,24 @@ Notifications.scheduleLocalNotification = function() {
return this.callNative('scheduleLocalNotification', arguments);
};

Notifications.cancelLocalNotifications = function() {
return this.callNative('cancelLocalNotifications', arguments);
Notifications.cancelLocalNotifications = function(userInfo) {
if ( Platform.OS === 'ios' ) {
return this.callNative('removePendingNotificationRequests', [[userInfo.id]]);
} else {
return this.callNative('cancelLocalNotifications', [userInfo]);
}
};

Notifications.clearLocalNotification = function() {
return this.callNative('clearLocalNotification', arguments);
};

Notifications.cancelAllLocalNotifications = function() {
return this.callNative('cancelAllLocalNotifications', arguments);
if ( Platform.OS === 'ios' ) {
return this.callNative('removeAllPendingNotificationRequests', arguments);
} else if (Platform.OS === 'android') {
return this.callNative('cancelAllLocalNotifications', arguments);
}
};

Notifications.setApplicationIconBadgeNumber = function() {
Expand Down Expand Up @@ -513,13 +516,12 @@ Notifications.getScheduledLocalNotifications = function(callback) {
if(Platform.OS === 'ios'){
mappedNotifications = notifications.map(notif => {
return ({
soundName: notif.soundName,
repeatInterval: notif.repeatInterval,
id: notif.userInfo?.id,
date: new Date(notif.fireDate),
number: notif?.applicationIconBadgeNumber,
message: notif?.alertBody,
title: notif?.alertTitle,
soundName: notif?.sound,
id: notif.id,
date: (notif.date ? new Date(notif.date) : null),
number: notif?.badge,
message: notif?.body,
title: notif?.title,
})
})
} else if(Platform.OS === 'android') {
Expand All @@ -539,7 +541,11 @@ Notifications.getScheduledLocalNotifications = function(callback) {
callback(mappedNotifications);
}

return this.callNative('getScheduledLocalNotifications', [mapNotifications]);
if(Platform.OS === 'ios'){
return this.callNative('getPendingNotificationRequests', [mapNotifications]);
} else {
return this.callNative('getScheduledLocalNotifications', [mapNotifications]);
}
}

Notifications.removeDeliveredNotifications = function() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"url": "git+ssh://git@github.com:zo0r/react-native-push-notification.git"
},
"peerDependencies": {
"@react-native-community/push-notification-ios": "^1.5.0",
"@react-native-community/push-notification-ios": "^1.8.0",
"react-native": ">=0.33"
},
"author": "zo0r <http://zo0r.me>",
Expand Down