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

Commit

Permalink
userInfo is now populated with id by default to allow operation bas…
Browse files Browse the repository at this point in the history
…ed on `id`
  • Loading branch information
Dallas62 committed Jun 9, 2020
1 parent 89ea506 commit 6bcc2a9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 59 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
```xml
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
```
- (iOS) `userInfo` is now populated with id by default to allow operation based on `id`.

### Features

Expand All @@ -34,6 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- (Android) Add `groupSummary` to allow grouping notifications. Based on [#1253](https://github.com/zo0r/react-native-push-notification/pull/1253)
- (Android) Add `channelId`, custom channel_id in android. Based on [#1159](https://github.com/zo0r/react-native-push-notification/pull/1159)
- (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)
- (Android/iOS) Add method getScheduledLocalNotifications()[#1466](https://github.com/zo0r/react-native-push-notification/pull/1466)

### Fixed

Expand Down
43 changes: 13 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,21 +413,19 @@ If you want to use a different default channel for remote notification, refer to
You can list available channels with:

```js
PushNotification.getChannels(function(channel_ids) {
PushNotification.getChannels(function (channel_ids) {
console.log(channel_ids); // ['channel_id_1']
});

```

### Channel exists

You can check if a channel exists with:

```js
PushNotification.channelExists(function(exists) {
PushNotification.channelExists(function (exists) {
console.log(exists); // true/false
});

```

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

```js
PushNotification.deleteChannel(channel_id);

```

## Cancelling notifications

### 1) cancelLocalNotifications

#### Android

The `id` parameter for `PushNotification.localNotification` is required for this operation. The id supplied will then be used for the cancel operation.

```javascript
// Android
PushNotification.localNotification({
...
id: '123'
Expand All @@ -457,19 +451,7 @@ PushNotification.localNotification({
PushNotification.cancelLocalNotifications({id: '123'});
```

#### IOS

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.

```javascript
// IOS
PushNotification.localNotification({
...
userInfo: { id: '123' }
...
});
PushNotification.cancelLocalNotifications({id: '123'});
```
**iOS: `userInfo` is populated `id` if not defined this allow the pervious method**

This comment has been minimized.

Copy link
@lukebars

lukebars Jun 9, 2020

Contributor

@Dallas62 typo here pervious :shipit:


### 2) cancelAllLocalNotifications

Expand Down Expand Up @@ -540,15 +522,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. |
| 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. |
| number | number | App notification badge count number. |

## Abandon Permissions

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

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

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

Make sure you have the receiver in `AndroidManifest.xml`:

```xml
<receiver android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationActions" />
```
Expand Down
67 changes: 38 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,17 @@ Notifications.unregister = function() {
* @param {Object} details.userInfo - iOS ONLY: The userInfo used in the notification alert.
*/
Notifications.localNotification = function(details) {
if ( Platform.OS === 'ios' ) {
if (details && typeof details.id === 'number') {
if (isNaN(details.id)) {
console.warn('NaN value has been passed as id');
delete details.id;
}
else {
details.id = '' + details.id;
}
}

if (Platform.OS === 'ios') {
// https://developer.apple.com/reference/uikit/uilocalnotification

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

if (details.userInfo) {
details.userInfo.id = details.userInfo.id || details.id;
}

// 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

Expand All @@ -161,17 +175,7 @@ Notifications.localNotification = function(details) {
userInfo: details.userInfo
});
} else {
if(details && typeof details.id === 'number') {
if(isNaN(details.id)) {
console.warn('NaN value has been passed as id');
delete details.id;
}
else {
details.id = '' + details.id;
}
}

if(details && typeof details.number === 'number') {
if (details && typeof details.number === 'number') {
if(isNaN(details.number)) {
console.warn('NaN value has been passed as number');
delete details.number;
Expand All @@ -181,7 +185,7 @@ Notifications.localNotification = function(details) {
}
}

if(details && typeof details.shortcutId === 'number') {
if (details && typeof details.shortcutId === 'number') {
if(isNaN(details.shortcutId)) {
console.warn('NaN value has been passed as shortcutId');
delete details.shortcutId;
Expand All @@ -205,13 +209,27 @@ Notifications.localNotification = function(details) {
* @param {Date} details.date - The date and time when the system should deliver the notification
*/
Notifications.localNotificationSchedule = function(details) {
if ( Platform.OS === 'ios' ) {
if (details && typeof details.id === 'number') {
if(isNaN(details.id)) {
console.warn('NaN value has been passed as id');
delete details.id;
}
else {
details.id = '' + details.id;
}
}

if (Platform.OS === 'ios') {
let soundName = details.soundName ? details.soundName : 'default'; // play sound (and vibrate) as default behaviour

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

if (details.userInfo) {
details.userInfo.id = details.userInfo.id || details.id;
}

const iosDetails = {
fireDate: details.date.toISOString(),
alertTitle: details.title,
Expand All @@ -223,7 +241,7 @@ Notifications.localNotificationSchedule = function(details) {
category: details.category,
};

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

Expand All @@ -233,18 +251,8 @@ Notifications.localNotificationSchedule = function(details) {
}
this.handler.scheduleLocalNotification(iosDetails);
} else {
if(details && typeof details.id === 'number') {
if(isNaN(details.id)) {
console.warn('NaN value has been passed as id');
delete details.id;
}
else {
details.id = '' + details.id;
}
}

if(details && typeof details.number === 'number') {
if(isNaN(details.number)) {
if (details && typeof details.number === 'number') {
if (isNaN(details.number)) {
console.warn('NaN value has been passed as number');
delete details.number;
}
Expand All @@ -253,8 +261,8 @@ Notifications.localNotificationSchedule = function(details) {
}
}

if(details && typeof details.shortcutId === 'number') {
if(isNaN(details.shortcutId)) {
if (details && typeof details.shortcutId === 'number') {
if (isNaN(details.shortcutId)) {
console.warn('NaN value has been passed as shortcutId');
delete details.shortcutId;
}
Expand Down Expand Up @@ -316,6 +324,7 @@ Notifications._onNotification = function(data, isFromBackground = null) {
if ( this.onNotification !== false ) {
if ( Platform.OS === 'ios' ) {
this.onNotification({
id: notif.userInfo?.id,
foreground: ! isFromBackground,
userInteraction: isFromBackground,
message: data.getMessage(),
Expand Down

0 comments on commit 6bcc2a9

Please sign in to comment.