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

Commit 6bcc2a9

Browse files
committed
userInfo is now populated with id by default to allow operation based on id
1 parent 89ea506 commit 6bcc2a9

File tree

3 files changed

+53
-59
lines changed

3 files changed

+53
-59
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1717
```xml
1818
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
1919
```
20+
- (iOS) `userInfo` is now populated with id by default to allow operation based on `id`.
2021

2122
### Features
2223

@@ -34,6 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
3435
- (Android) Add `groupSummary` to allow grouping notifications. Based on [#1253](https://github.com/zo0r/react-native-push-notification/pull/1253)
3536
- (Android) Add `channelId`, custom channel_id in android. Based on [#1159](https://github.com/zo0r/react-native-push-notification/pull/1159)
3637
- (iOS) Add fire date in notification response, NOTE: `push-notification-ios` in version `> 1.2.0` [#1345](https://github.com/zo0r/react-native-push-notification/pull/1345)
38+
- (Android/iOS) Add method getScheduledLocalNotifications()[#1466](https://github.com/zo0r/react-native-push-notification/pull/1466)
3739

3840
### Fixed
3941

README.md

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -413,21 +413,19 @@ If you want to use a different default channel for remote notification, refer to
413413
You can list available channels with:
414414

415415
```js
416-
PushNotification.getChannels(function(channel_ids) {
416+
PushNotification.getChannels(function (channel_ids) {
417417
console.log(channel_ids); // ['channel_id_1']
418418
});
419-
420419
```
421420

422421
### Channel exists
423422

424423
You can check if a channel exists with:
425424

426425
```js
427-
PushNotification.channelExists(function(exists) {
426+
PushNotification.channelExists(function (exists) {
428427
console.log(exists); // true/false
429428
});
430-
431429
```
432430

433431
### List channels
@@ -436,19 +434,15 @@ You can list available channels with:
436434

437435
```js
438436
PushNotification.deleteChannel(channel_id);
439-
440437
```
441438

442439
## Cancelling notifications
443440

444441
### 1) cancelLocalNotifications
445442

446-
#### Android
447-
448443
The `id` parameter for `PushNotification.localNotification` is required for this operation. The id supplied will then be used for the cancel operation.
449444

450445
```javascript
451-
// Android
452446
PushNotification.localNotification({
453447
...
454448
id: '123'
@@ -457,19 +451,7 @@ PushNotification.localNotification({
457451
PushNotification.cancelLocalNotifications({id: '123'});
458452
```
459453

460-
#### IOS
461-
462-
The `userInfo` parameter for `PushNotification.localNotification` is required for this operation and must contain an `id` parameter. The id supplied will then be used for the cancel operation.
463-
464-
```javascript
465-
// IOS
466-
PushNotification.localNotification({
467-
...
468-
userInfo: { id: '123' }
469-
...
470-
});
471-
PushNotification.cancelLocalNotifications({id: '123'});
472-
```
454+
**iOS: `userInfo` is populated `id` if not defined this allow the pervious method**
473455

474456
### 2) cancelAllLocalNotifications
475457

@@ -540,15 +522,15 @@ Provides you with a list of the app’s scheduled local notifications that are y
540522

541523
Returns an array of local scheduled notification objects containing:
542524

543-
| Name | Type | Description |
544-
| -------- | -------- | -------- | ----------------------------------------------------------- |
545-
| id | number | The identifier of this notification. |
546-
| date | Date | The fire date of this notification. |
547-
| title | string | The title of this notification. |
548-
| message | string | The message body of this notification. |
549-
| soundName | string | The sound name of this notification. |
525+
| Name | Type | Description |
526+
| -------------- | ------ | ----------------------------------------- |
527+
| id | number | The identifier of this notification. |
528+
| date | Date | The fire date of this notification. |
529+
| title | string | The title of this notification. |
530+
| message | string | The message body of this notification. |
531+
| soundName | string | The sound name of this notification. |
550532
| repeatInterval | number | The repeat interval of this notification. |
551-
| number | number | App notification badge count number. |
533+
| number | number | App notification badge count number. |
552534

553535
## Abandon Permissions
554536

@@ -621,9 +603,10 @@ This is done by specifying an `actions` parameters while configuring the local n
621603

622604
For e.g. `actions: ['Accept', 'Reject']`
623605

624-
When you handle actions in background (`invokeApp: false`), you can open the application and pass the initial notification by using use `PushNotification.invokeApp(notification)`.
606+
When you handle actions in background (`invokeApp: false`), you can open the application and pass the initial notification by using use `PushNotification.invokeApp(notification)`.
625607

626608
Make sure you have the receiver in `AndroidManifest.xml`:
609+
627610
```xml
628611
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
629612
```

index.js

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,17 @@ Notifications.unregister = function() {
139139
* @param {Object} details.userInfo - iOS ONLY: The userInfo used in the notification alert.
140140
*/
141141
Notifications.localNotification = function(details) {
142-
if ( Platform.OS === 'ios' ) {
142+
if (details && typeof details.id === 'number') {
143+
if (isNaN(details.id)) {
144+
console.warn('NaN value has been passed as id');
145+
delete details.id;
146+
}
147+
else {
148+
details.id = '' + details.id;
149+
}
150+
}
151+
152+
if (Platform.OS === 'ios') {
143153
// https://developer.apple.com/reference/uikit/uilocalnotification
144154

145155
let soundName = details.soundName ? details.soundName : 'default'; // play sound (and vibrate) as default behaviour
@@ -148,6 +158,10 @@ Notifications.localNotification = function(details) {
148158
soundName = ''; // empty string results in no sound (and no vibration)
149159
}
150160

161+
if (details.userInfo) {
162+
details.userInfo.id = details.userInfo.id || details.id;
163+
}
164+
151165
// for valid fields see: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html
152166
// alertTitle only valid for apple watch: https://developer.apple.com/library/ios/documentation/iPhone/Reference/UILocalNotification_Class/#//apple_ref/occ/instp/UILocalNotification/alertTitle
153167

@@ -161,17 +175,7 @@ Notifications.localNotification = function(details) {
161175
userInfo: details.userInfo
162176
});
163177
} else {
164-
if(details && typeof details.id === 'number') {
165-
if(isNaN(details.id)) {
166-
console.warn('NaN value has been passed as id');
167-
delete details.id;
168-
}
169-
else {
170-
details.id = '' + details.id;
171-
}
172-
}
173-
174-
if(details && typeof details.number === 'number') {
178+
if (details && typeof details.number === 'number') {
175179
if(isNaN(details.number)) {
176180
console.warn('NaN value has been passed as number');
177181
delete details.number;
@@ -181,7 +185,7 @@ Notifications.localNotification = function(details) {
181185
}
182186
}
183187

184-
if(details && typeof details.shortcutId === 'number') {
188+
if (details && typeof details.shortcutId === 'number') {
185189
if(isNaN(details.shortcutId)) {
186190
console.warn('NaN value has been passed as shortcutId');
187191
delete details.shortcutId;
@@ -205,13 +209,27 @@ Notifications.localNotification = function(details) {
205209
* @param {Date} details.date - The date and time when the system should deliver the notification
206210
*/
207211
Notifications.localNotificationSchedule = function(details) {
208-
if ( Platform.OS === 'ios' ) {
212+
if (details && typeof details.id === 'number') {
213+
if(isNaN(details.id)) {
214+
console.warn('NaN value has been passed as id');
215+
delete details.id;
216+
}
217+
else {
218+
details.id = '' + details.id;
219+
}
220+
}
221+
222+
if (Platform.OS === 'ios') {
209223
let soundName = details.soundName ? details.soundName : 'default'; // play sound (and vibrate) as default behaviour
210224

211225
if (details.hasOwnProperty('playSound') && !details.playSound) {
212226
soundName = ''; // empty string results in no sound (and no vibration)
213227
}
214228

229+
if (details.userInfo) {
230+
details.userInfo.id = details.userInfo.id || details.id;
231+
}
232+
215233
const iosDetails = {
216234
fireDate: details.date.toISOString(),
217235
alertTitle: details.title,
@@ -223,7 +241,7 @@ Notifications.localNotificationSchedule = function(details) {
223241
category: details.category,
224242
};
225243

226-
if(details.number) {
244+
if (details.number) {
227245
iosDetails.applicationIconBadgeNumber = parseInt(details.number, 10);
228246
}
229247

@@ -233,18 +251,8 @@ Notifications.localNotificationSchedule = function(details) {
233251
}
234252
this.handler.scheduleLocalNotification(iosDetails);
235253
} else {
236-
if(details && typeof details.id === 'number') {
237-
if(isNaN(details.id)) {
238-
console.warn('NaN value has been passed as id');
239-
delete details.id;
240-
}
241-
else {
242-
details.id = '' + details.id;
243-
}
244-
}
245-
246-
if(details && typeof details.number === 'number') {
247-
if(isNaN(details.number)) {
254+
if (details && typeof details.number === 'number') {
255+
if (isNaN(details.number)) {
248256
console.warn('NaN value has been passed as number');
249257
delete details.number;
250258
}
@@ -253,8 +261,8 @@ Notifications.localNotificationSchedule = function(details) {
253261
}
254262
}
255263

256-
if(details && typeof details.shortcutId === 'number') {
257-
if(isNaN(details.shortcutId)) {
264+
if (details && typeof details.shortcutId === 'number') {
265+
if (isNaN(details.shortcutId)) {
258266
console.warn('NaN value has been passed as shortcutId');
259267
delete details.shortcutId;
260268
}
@@ -316,6 +324,7 @@ Notifications._onNotification = function(data, isFromBackground = null) {
316324
if ( this.onNotification !== false ) {
317325
if ( Platform.OS === 'ios' ) {
318326
this.onNotification({
327+
id: notif.userInfo?.id,
319328
foreground: ! isFromBackground,
320329
userInteraction: isFromBackground,
321330
message: data.getMessage(),

0 commit comments

Comments
 (0)