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

Patch 2 #1

Merged
merged 6 commits into from
Jun 10, 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
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**

### 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
2 changes: 1 addition & 1 deletion example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export default class App extends Component {
<TouchableOpacity
style={styles.button}
onPress={() => {
this.notif.getScheduledLocalNotifications(notifs => console.log(notifs));
this.notif.getScheduledLocalNotifications().then(notifs => console.log(notifs));
}}>
<Text>Console.Log Scheduled Local Notifications</Text>
</TouchableOpacity>
Expand Down
145 changes: 82 additions & 63 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,12 @@ 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;
} else {
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 +177,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 +187,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 +211,29 @@ 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;
} else {
details.userInfo = {id: details.id};
}

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

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

Expand All @@ -233,18 +255,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 +265,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 +328,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 Expand Up @@ -439,40 +452,46 @@ Notifications.getDeliveredNotifications = function() {
return this.callNative('getDeliveredNotifications', arguments);
}

Notifications.getScheduledLocalNotifications = function(callback) {
const mapNotifications = (notifications) => {
let mappedNotifications = [];
if(notifications?.length > 0) {
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,
})
})
} else if(Platform.OS === 'android') {
mappedNotifications = notifications.map(notif => {
return ({
soundName: notif.soundName,
repeatInterval: notif.repeatInterval,
id: notif.id,
date: new Date(notif.date),
number: notif.number,
message: notif.message,
title: notif.title,
})
})
}
}
callback(mappedNotifications);
}

return this.callNative('getScheduledLocalNotifications', [mapNotifications]);
Notifications.getScheduledLocalNotifications = function() {
return new Promise((resolve, reject) => {
const mapNotifications = (notifications) => {
let mappedNotifications = [];
if(notifications?.length > 0) {
if(Platform.OS === 'ios'){
mappedNotifications = notifications.map(notif => {
console.tron.log(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,
})
})
} else if(Platform.OS === 'android') {
mappedNotifications = notifications.map(notif => {
return ({
soundName: notif.soundName,
repeatInterval: notif.repeatInterval,
id: notif.id,
date: new Date(notif.date),
number: notif.number,
message: notif.message,
title: notif.title,
})
})
}
}
resolve(mappedNotifications);
}
try{
this.callNative('getScheduledLocalNotifications', [mapNotifications]);
} catch(e) {
reject(e);
}
})
}

Notifications.removeDeliveredNotifications = function() {
Expand Down