From 17ad0aec909a6c489b3082e8f148d905de1f1bbd Mon Sep 17 00:00:00 2001 From: Boris Tacyniak Date: Thu, 19 Aug 2021 10:49:03 +0200 Subject: [PATCH] Upgrade to 8.0.0 --- CHANGELOG.md | 16 ++++++ README.md | 7 +-- .../modules/RNPushNotification.java | 6 +- .../modules/RNPushNotificationAttributes.java | 56 ------------------- .../modules/RNPushNotificationHelper.java | 18 +----- example/NotifService.js | 2 +- example/ios/Podfile.lock | 4 +- index.js | 48 +++++++++++----- package.json | 4 +- 9 files changed, 62 insertions(+), 99 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc1dda977..f98d8589b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,22 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html ### Fixed +## [8.0.0] 2021-08-19 + +### Breaking changes + +- `userInfo` is no more populated with the `id` of the notification, initialy included to cancel scheduled notifications. This change will probably not impact you. +- Rename `cancelLocalNotifications` to `cancelLocalNotification` (deprecation notice). + +### Features + +- (iOS) upgrade `@react-native-community/push-notification-ios` to version [1.9.0](https://github.com/react-native-push-notification/ios/releases/tag/v1.9.0) +- `picture` is now support for both Android and iOS, (alias of `bigPictureUrl` for Android). + +### Fixed + +- (Android): Fix bug cancelLocalNotifications() does not work on Android [#2100](https://github.com/zo0r/react-native-push-notification/issues/2100) + ## [7.4.0] 2021-06-24 ### Features diff --git a/README.md b/README.md index 5c341253f..5b3473593 100644 --- a/README.md +++ b/README.md @@ -342,6 +342,7 @@ PushNotification.localNotification({ id: 0, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID title: "My Notification Title", // (optional) message: "My Notification Message", // (required) + picture: "https://www.example.tld/picture.jpg", // (optional) Display an picture with the notification, alias of `bigPictureUrl` for Android. default: undefined userInfo: {}, // (optional) default: {} (using null throws a JSON value '' error) playSound: false, // (optional) default: true soundName: "default", // (optional) Sound to play when the notification is shown. Value of 'default' plays the default sound. It can be set to a custom sound such as 'android.resource://com.xyz/raw/my_sound'. It will look for the 'my_sound' audio file in 'res/raw' directory and play it. default: 'default' (default sound is played) @@ -481,7 +482,7 @@ PushNotification.deleteChannel(channel_id); ## Cancelling notifications -### 1) cancelLocalNotifications +### 1) cancelLocalNotification The `id` parameter for `PushNotification.localNotification` is required for this operation. The id supplied will then be used for the cancel operation. @@ -491,11 +492,9 @@ PushNotification.localNotification({ id: '123' ... }); -PushNotification.cancelLocalNotifications({id: '123'}); +PushNotification.cancelLocalNotification('123'); ``` -**iOS: `userInfo` is populated `id` if not defined this allow the previous method** - ### 2) cancelAllLocalNotifications ```javascript diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java index da9cc767c..d16268059 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java @@ -226,11 +226,11 @@ public void cancelAllLocalNotifications() { @ReactMethod /** - * Cancel scheduled notifications, and removes notifications from the notification centre. + * Cancel scheduled notification, and remove notification from the notification centre. * */ - public void cancelLocalNotifications(ReadableMap userInfo) { - mRNPushNotificationHelper.cancelScheduledNotification(userInfo); + public void cancelLocalNotification(String notification_id) { + mRNPushNotificationHelper.cancelScheduledNotification(notification_id); } @ReactMethod diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationAttributes.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationAttributes.java index a2d3d5657..1602daa0d 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationAttributes.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationAttributes.java @@ -190,62 +190,6 @@ public static RNPushNotificationAttributes fromJson(String notificationAttribute return new RNPushNotificationAttributes(jsonObject); } - /** - * User to find notifications: - *

- * - * @param userInfo map of fields to match - * @return true all fields in userInfo object match, false otherwise - */ - public boolean matches(ReadableMap userInfo) { - try { - if(this.userInfo == null) { - return false; - } - - JSONObject jsonObject = new JSONObject(this.userInfo); - - ReadableMapKeySetIterator iterator = userInfo.keySetIterator(); - while (iterator.hasNextKey()) { - String key = iterator.nextKey(); - - if (!jsonObject.has(key)) - return false; - - switch (userInfo.getType(key)) { - case Null: { - if (jsonObject.get(key) != null) - return false; - break; - } - case Boolean: { - if (userInfo.getBoolean(key) != jsonObject.getBoolean(key)) - return false; - break; - } - case Number: { - if ((userInfo.getDouble(key) != jsonObject.getDouble(key)) && (userInfo.getInt(key) != jsonObject.getInt(key))) - return false; - break; - } - case String: { - if (!userInfo.getString(key).equals(jsonObject.getString(key))) - return false; - break; - } - case Map: - return false;//there are no maps in the jsonObject - case Array: - return false;//there are no arrays in the jsonObject - } - } - } catch(JSONException e) { - return false; - } - - return true; - } - public Bundle toBundle() { Bundle bundle = new Bundle(); bundle.putString(ID, id); diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java index dc6c67491..4a606945c 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java @@ -775,23 +775,7 @@ public void cancelAllScheduledNotifications() { } } - public void cancelScheduledNotification(ReadableMap userInfo) { - for (String id : scheduledNotificationsPersistence.getAll().keySet()) { - try { - String notificationAttributesJson = scheduledNotificationsPersistence.getString(id, null); - if (notificationAttributesJson != null) { - RNPushNotificationAttributes notificationAttributes = fromJson(notificationAttributesJson); - if (notificationAttributes.matches(userInfo)) { - cancelScheduledNotification(id); - } - } - } catch (JSONException e) { - Log.w(LOG_TAG, "Problem dealing with scheduled notification " + id, e); - } - } - } - - private void cancelScheduledNotification(String notificationIDString) { + public void cancelScheduledNotification(String notificationIDString) { Log.i(LOG_TAG, "Cancelling notification: " + notificationIDString); // remove it from the alarm manger schedule diff --git a/example/NotifService.js b/example/NotifService.js index 03a0b7df5..481f628a9 100644 --- a/example/NotifService.js +++ b/example/NotifService.js @@ -157,7 +157,7 @@ export default class NotifService { } cancelNotif() { - PushNotification.cancelLocalNotifications({id: '' + this.lastId}); + PushNotification.cancelLocalNotification(this.lastId); } cancelAll() { diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 06c11226a..6801cbc8e 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -245,7 +245,7 @@ PODS: - React-Core (= 0.63.3) - React-cxxreact (= 0.63.3) - React-jsi (= 0.63.3) - - RNCPushNotificationIOS (1.8.0): + - RNCPushNotificationIOS (1.9.0): - React-Core - Yoga (1.14.0) @@ -367,7 +367,7 @@ SPEC CHECKSUMS: React-RCTText: 65a6de06a7389098ce24340d1d3556015c38f746 React-RCTVibration: 8e9fb25724a0805107fc1acc9075e26f814df454 ReactCommon: 4167844018c9ed375cc01a843e9ee564399e53c3 - RNCPushNotificationIOS: 61a7c72bd1ebad3568025957d001e0f0e7b32191 + RNCPushNotificationIOS: 5bffde624d1fd15bfc8b2fd202b012a517a6dc9b Yoga: 7d13633d129fd179e01b8953d38d47be90db185a PODFILE CHECKSUM: 9f7efe26f7ad5184f28ac62478069370942924e2 diff --git a/index.js b/index.js index 4567f95c3..594c12e32 100644 --- a/index.js +++ b/index.js @@ -155,12 +155,6 @@ Notifications.localNotification = function({...details}) { } } - if (details.userInfo) { - details.userInfo.id = details.userInfo.id || details.id; - } else { - details.userInfo = {id: details.id}; - } - if (Platform.OS === 'ios') { // https://developer.apple.com/reference/uikit/uilocalnotification @@ -170,6 +164,11 @@ Notifications.localNotification = function({...details}) { soundName = ''; // empty string results in no sound (and no vibration) } + if(details.picture) { + details.userInfo = details.userInfo || {}; + details.userInfo.image = details.picture; + } + // for valid fields see: https://github.com/react-native-push-notification-ios/push-notification-ios#addnotificationrequest this.handler.addNotificationRequest({ @@ -212,6 +211,10 @@ Notifications.localNotification = function({...details}) { details.userInfo = JSON.stringify(details.userInfo); } + if(details.picture && !details.bigPictureUrl) { + details.bigPictureUrl = details.picture; + } + this.handler.presentLocalNotification(details); } }; @@ -236,12 +239,6 @@ Notifications.localNotificationSchedule = function({...details}) { } } - if (details.userInfo) { - details.userInfo.id = details.userInfo.id || details.id; - } else { - details.userInfo = {id: details.id}; - } - if (Platform.OS === 'ios') { let soundName = details.soundName ? details.soundName : 'default'; // play sound (and vibrate) as default behaviour @@ -249,6 +246,11 @@ Notifications.localNotificationSchedule = function({...details}) { soundName = ''; // empty string results in no sound (and no vibration) } + if(details.picture) { + details.userInfo = details.userInfo || {}; + details.userInfo.image = details.picture; + } + const iosDetails = { id: (!details.id ? Math.floor(Math.random() * Math.pow(2, 32)).toString() : details.id), fireDate: details.date.toISOString(), @@ -296,6 +298,10 @@ Notifications.localNotificationSchedule = function({...details}) { details.userInfo = JSON.stringify(details.userInfo); } + if(details.picture && !details.bigPictureUrl) { + details.bigPictureUrl = details.picture; + } + details.fireDate = details.date.getTime(); delete details.date; // ignore iOS only repeatType @@ -472,10 +478,24 @@ Notifications.scheduleLocalNotification = function() { }; Notifications.cancelLocalNotifications = function(userInfo) { + console.warn('This method is now deprecated, please use `cancelLocalNotification` (remove the ending `s`).'); + + return this.cancelLocalNotification(userInfo); +}; + +Notifications.cancelLocalNotification = function(notificationId) { + if(typeof notificationId === 'object') { + notificationId = notificationId?.id; + } + + if(typeof notificationId === 'number') { + notificationId = '' + notificationId; + } + if ( Platform.OS === 'ios' ) { - return this.callNative('removePendingNotificationRequests', [[userInfo.id]]); + return this.callNative('removePendingNotificationRequests', [[notificationId]]); } else { - return this.callNative('cancelLocalNotifications', [userInfo]); + return this.callNative('cancelLocalNotification', [notificationId]); } }; diff --git a/package.json b/package.json index c5908a521..90f9f4b02 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-push-notification", - "version": "7.4.0", + "version": "8.0.0", "description": "React Native Local and Remote Notifications", "main": "index.js", "scripts": { @@ -24,7 +24,7 @@ "url": "git+ssh://git@github.com:zo0r/react-native-push-notification.git" }, "peerDependencies": { - "@react-native-community/push-notification-ios": "^1.8.0", + "@react-native-community/push-notification-ios": "^1.9.0", "react-native": ">=0.33" }, "author": "zo0r ",