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

Commit f2ffd97

Browse files
author
Boris Tacyniak
authored
Merge pull request #1751 from nbolender/remove-deprecated-local-notifications-ios
Replace deprecated local notification methods on iOS
2 parents e370aeb + 28cf219 commit f2ffd97

File tree

5 files changed

+64
-50
lines changed

5 files changed

+64
-50
lines changed

.npmignore

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,13 @@ local.properties
6262
example
6363

6464
# Git
65-
.git
65+
.git
66+
67+
# GitHub
68+
.github/*
69+
70+
# Docs
71+
submitting-a-pull-request.md
72+
73+
# Vscode
74+
.vscode/*

README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public class MainApplication extends Application implements ReactApplication {
219219

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

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

338338
/* iOS only properties */
339-
alertAction: "view", // (optional) default: view
340339
category: "", // (optional) default: empty string
341340

342341
/* iOS and Android properties */
@@ -498,20 +497,18 @@ PushNotification.cancelLocalNotifications({id: '123'});
498497

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

501-
_NOTE: there is currently no api for removing specific notification alerts from the notification centre._
502-
503500
### 3) removeAllDeliveredNotifications
504501

505502
```javascript
506-
PushNotificationIOS.removeAllDeliveredNotifications();
503+
PushNotification.removeAllDeliveredNotifications();
507504
```
508505

509506
Remove all delivered notifications from Notification Center
510507

511508
### 4) getDeliveredNotifications
512509

513510
```javascript
514-
PushNotificationIOS.getDeliveredNotifications(callback);
511+
PushNotification.getDeliveredNotifications(callback);
515512
```
516513

517514
Provides you with a list of the app’s notifications that are still displayed in Notification Center
@@ -534,7 +531,7 @@ A delivered notification is an object containing:
534531
### 5) removeDeliveredNotifications
535532

536533
```javascript
537-
PushNotificationIOS.removeDeliveredNotifications(identifiers);
534+
PushNotification.removeDeliveredNotifications(identifiers);
538535
```
539536

540537
Removes the specified notifications from Notification Center
@@ -548,7 +545,7 @@ Removes the specified notifications from Notification Center
548545
### 6) getScheduledLocalNotifications
549546

550547
```javascript
551-
PushNotificationIOS.getScheduledLocalNotifications(callback);
548+
PushNotification.getScheduledLocalNotifications(callback);
552549
```
553550

554551
Provides you with a list of the app’s scheduled local notifications that are yet to be displayed
@@ -561,15 +558,15 @@ Provides you with a list of the app’s scheduled local notifications that are y
561558

562559
Returns an array of local scheduled notification objects containing:
563560

564-
| Name | Type | Description |
565-
| -------------- | ------ | ----------------------------------------- |
566-
| id | number | The identifier of this notification. |
567-
| date | Date | The fire date of this notification. |
568-
| title | string | The title of this notification. |
569-
| message | string | The message body of this notification. |
570-
| soundName | string | The sound name of this notification. |
571-
| repeatInterval | number | The repeat interval of this notification. |
572-
| number | number | App notification badge count number. |
561+
| Name | Type | Description |
562+
| -------------- | ------ | -------------------------------------------------------- |
563+
| id | number | The identifier of this notification. |
564+
| date | Date | The fire date of this notification. |
565+
| title | string | The title of this notification. |
566+
| message | string | The message body of this notification. |
567+
| soundName | string | The sound name of this notification. |
568+
| repeatInterval | number | (Android only) The repeat interval of this notification. |
569+
| number | number | App notification badge count number. |
573570

574571
## Abandon Permissions
575572

@@ -632,6 +629,10 @@ https://developer.android.com/training/monitoring-device-state/doze-standby
632629

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

632+
### iOS
633+
Property `repeatType` can only be `day`.
634+
635+
### Android
635636
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.
636637

637638
## Notification Actions

example/NotifService.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ export default class NotifService {
9393
timeoutAfter: null, // (optional) Specifies a duration in milliseconds after which this notification should be canceled, if it is not already canceled, default: null
9494

9595
/* iOS only properties */
96-
alertAction: 'view', // (optional) default: view
9796
category: '', // (optional) default: empty string
9897

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

137136
/* iOS only properties */
138-
alertAction: 'view', // (optional) default: view
139137
category: '', // (optional) default: empty string
140138

141139
/* iOS and Android properties */

index.js

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,14 @@ Notifications.localNotification = function(details) {
176176
}
177177

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

181-
this.handler.presentLocalNotification({
182-
alertTitle: details.title,
183-
alertBody: details.message,
184-
alertAction: details.alertAction,
180+
this.handler.addNotificationRequest({
181+
id: (!details.id ? Math.floor(Math.random() * Math.pow(2, 32)).toString() : details.id),
182+
title: details.title,
183+
body: details.message,
184+
badge: details.number,
185+
sound: soundName,
185186
category: details.category,
186-
soundName: soundName,
187-
applicationIconBadgeNumber: details.number,
188187
userInfo: details.userInfo
189188
});
190189
} else {
@@ -250,25 +249,21 @@ Notifications.localNotificationSchedule = function(details) {
250249
}
251250

252251
const iosDetails = {
252+
id: (!details.id ? Math.floor(Math.random() * Math.pow(2, 32)).toString() : details.id),
253253
fireDate: details.date.toISOString(),
254-
alertTitle: details.title,
255-
alertBody: details.message,
254+
title: details.title,
255+
body: details.message,
256+
sound: soundName,
256257
category: details.category,
257-
soundName: soundName,
258258
userInfo: details.userInfo,
259-
repeatInterval: details.repeatType,
260-
category: details.category,
259+
repeats: (details.repeatType && details.repeatType == "day"),
261260
};
262261

263262
if (details.number) {
264-
iosDetails.applicationIconBadgeNumber = parseInt(details.number, 10);
263+
iosDetails.badge = parseInt(details.number, 10);
265264
}
266265

267-
// ignore Android only repeatType
268-
if (!details.repeatType || details.repeatType === 'time') {
269-
delete iosDetails.repeatInterval;
270-
}
271-
this.handler.scheduleLocalNotification(iosDetails);
266+
this.handler.addNotificationRequest(iosDetails);
272267
} else {
273268
if (details && typeof details.number === 'number') {
274269
if (isNaN(details.number)) {
@@ -456,16 +451,24 @@ Notifications.scheduleLocalNotification = function() {
456451
return this.callNative('scheduleLocalNotification', arguments);
457452
};
458453

459-
Notifications.cancelLocalNotifications = function() {
460-
return this.callNative('cancelLocalNotifications', arguments);
454+
Notifications.cancelLocalNotifications = function(userInfo) {
455+
if ( Platform.OS === 'ios' ) {
456+
return this.callNative('removePendingNotificationRequests', [[userInfo.id]]);
457+
} else {
458+
return this.callNative('cancelLocalNotifications', [userInfo]);
459+
}
461460
};
462461

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

467466
Notifications.cancelAllLocalNotifications = function() {
468-
return this.callNative('cancelAllLocalNotifications', arguments);
467+
if ( Platform.OS === 'ios' ) {
468+
return this.callNative('removeAllPendingNotificationRequests', arguments);
469+
} else if (Platform.OS === 'android') {
470+
return this.callNative('cancelAllLocalNotifications', arguments);
471+
}
469472
};
470473

471474
Notifications.setApplicationIconBadgeNumber = function() {
@@ -513,13 +516,12 @@ Notifications.getScheduledLocalNotifications = function(callback) {
513516
if(Platform.OS === 'ios'){
514517
mappedNotifications = notifications.map(notif => {
515518
return ({
516-
soundName: notif.soundName,
517-
repeatInterval: notif.repeatInterval,
518-
id: notif.userInfo?.id,
519-
date: new Date(notif.fireDate),
520-
number: notif?.applicationIconBadgeNumber,
521-
message: notif?.alertBody,
522-
title: notif?.alertTitle,
519+
soundName: notif?.sound,
520+
id: notif.id,
521+
date: (notif.date ? new Date(notif.date) : null),
522+
number: notif?.badge,
523+
message: notif?.body,
524+
title: notif?.title,
523525
})
524526
})
525527
} else if(Platform.OS === 'android') {
@@ -539,7 +541,11 @@ Notifications.getScheduledLocalNotifications = function(callback) {
539541
callback(mappedNotifications);
540542
}
541543

542-
return this.callNative('getScheduledLocalNotifications', [mapNotifications]);
544+
if(Platform.OS === 'ios'){
545+
return this.callNative('getPendingNotificationRequests', [mapNotifications]);
546+
} else {
547+
return this.callNative('getScheduledLocalNotifications', [mapNotifications]);
548+
}
543549
}
544550

545551
Notifications.removeDeliveredNotifications = function() {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"url": "git+ssh://git@github.com:zo0r/react-native-push-notification.git"
2525
},
2626
"peerDependencies": {
27-
"@react-native-community/push-notification-ios": "^1.5.0",
27+
"@react-native-community/push-notification-ios": "^1.8.0",
2828
"react-native": ">=0.33"
2929
},
3030
"author": "zo0r <http://zo0r.me>",

0 commit comments

Comments
 (0)