From c90fc6eb576ff64e128c31f839914772215b873d Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas <46403446+lukebars@users.noreply.github.com> Date: Wed, 10 Jun 2020 14:26:35 +0300 Subject: [PATCH 01/18] return a Promise in getScheduledLocalNotifications --- index.js | 74 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/index.js b/index.js index 6f17de212..e71873e7e 100644 --- a/index.js +++ b/index.js @@ -452,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() { From a5cdcdae33a5f6717f4f1b2ca5a37e35f29f968f Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Wed, 10 Jun 2020 14:30:34 +0300 Subject: [PATCH 02/18] updated example/App.js --- example/App.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/App.js b/example/App.js index a4c8458b0..d6bebc85c 100644 --- a/example/App.js +++ b/example/App.js @@ -108,7 +108,7 @@ export default class App extends Component { { - this.notif.getScheduledLocalNotifications(notifs => console.log(notifs)); + this.notif.getScheduledLocalNotifications().then(notifs => console.log(notifs)); }}> Console.Log Scheduled Local Notifications From 9aa88d68fcd5bb0421e9b08a1d05b42887903a93 Mon Sep 17 00:00:00 2001 From: Boris Tacyniak Date: Wed, 1 Jul 2020 18:28:16 +0200 Subject: [PATCH 03/18] Create FUNDING.yml --- .github/FUNDING.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 000000000..29e999ea9 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,2 @@ +github: [Dallas62] +custom: [https://www.buymeacoffee.com/Dallas62] From da34ac1f8d31e1bbf757255f941b70854c4a2d40 Mon Sep 17 00:00:00 2001 From: Boris Tacyniak Date: Wed, 1 Jul 2020 18:29:25 +0200 Subject: [PATCH 04/18] Fix custom link --- .github/FUNDING.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 29e999ea9..105898580 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,2 +1,2 @@ github: [Dallas62] -custom: [https://www.buymeacoffee.com/Dallas62] +custom: ["https://www.buymeacoffee.com/Dallas62"] From 86af98dd2126f20cffeb8af5eb2c5a9cf9e47074 Mon Sep 17 00:00:00 2001 From: Boris Tacyniak Date: Mon, 6 Jul 2020 11:32:17 +0200 Subject: [PATCH 05/18] Bump package.json to 4.0.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 619625482..1a35e0136 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-push-notification", - "version": "3.5.2", + "version": "4.0.0", "description": "React Native Local and Remote Notifications", "main": "index.js", "scripts": { From 2816253ba64a97a30378afb89c477f4cface6990 Mon Sep 17 00:00:00 2001 From: Brian Rubio Date: Mon, 6 Jul 2020 19:21:09 -0500 Subject: [PATCH 06/18] Update CHANGELOG.md Fixed version from 4.4.0 to 4.0.0 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a267b11e..394d594df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Fixed -## [4.4.0] 2020-07-06 +## [4.0.0] 2020-07-06 ### Breaking changes From 74e9b8bf443f952d00604ce5153ac132d490b1a0 Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas <46403446+lukebars@users.noreply.github.com> Date: Wed, 8 Jul 2020 17:12:14 +0300 Subject: [PATCH 07/18] add data to android notification when scheduling --- index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/index.js b/index.js index e71db401f..3583018a0 100644 --- a/index.js +++ b/index.js @@ -190,6 +190,10 @@ Notifications.localNotification = function(details) { } } + if(!details.data && details.userInfo){ + details.data = details.userInfo; + } + if (details && typeof details.shortcutId === 'number') { if(isNaN(details.shortcutId)) { console.warn('NaN value has been passed as shortcutId'); @@ -277,6 +281,10 @@ Notifications.localNotificationSchedule = function(details) { details.shortcutId = '' + details.shortcutId; } } + + if(!details.data && details.userInfo){ + details.data = details.userInfo; + } if(details && Array.isArray(details.actions)) { details.actions = JSON.stringify(details.actions); From 454af8cbba7081029db192ad1c83725e5e4d3bdb Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Wed, 8 Jul 2020 17:21:39 +0300 Subject: [PATCH 08/18] "@react-native-community/push-notification-ios": "^1.3.0" --- example/ios/Podfile.lock | 6 +++--- example/package.json | 2 +- package.json | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index b3adad45f..2f44b3dad 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -292,7 +292,7 @@ PODS: - React-cxxreact (= 0.62.2) - React-jsi (= 0.62.2) - ReactCommon/callinvoker (= 0.62.2) - - RNCPushNotificationIOS (1.2.2): + - RNCPushNotificationIOS (1.3.0): - React - Yoga (1.14.0) - YogaKit (1.18.1): @@ -453,10 +453,10 @@ SPEC CHECKSUMS: React-RCTText: fae545b10cfdb3d247c36c56f61a94cfd6dba41d React-RCTVibration: 4356114dbcba4ce66991096e51a66e61eda51256 ReactCommon: ed4e11d27609d571e7eee8b65548efc191116eb3 - RNCPushNotificationIOS: 4c97a36dbec42dba411cc35e6dac25e34a805fde + RNCPushNotificationIOS: d5fd66aed4e03c6491ca0c6111a03d4f6455ff6c Yoga: 3ebccbdd559724312790e7742142d062476b698e YogaKit: f782866e155069a2cca2517aafea43200b01fd5a PODFILE CHECKSUM: 56c2537f71f3f02200d6918c542a8e89a0b422fa -COCOAPODS: 1.9.2 +COCOAPODS: 1.9.3 diff --git a/example/package.json b/example/package.json index 606b5d146..091ea7bee 100644 --- a/example/package.json +++ b/example/package.json @@ -11,7 +11,7 @@ "pod-install": "cd ios && pod install" }, "dependencies": { - "@react-native-community/push-notification-ios": "^1.2.2", + "@react-native-community/push-notification-ios": "^1.3.0", "react": "16.11.0", "react-native": "0.62.2", "react-native-push-notification": "git+https://git@github.com/zo0r/react-native-push-notification.git" diff --git a/package.json b/package.json index 619625482..05b97a682 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "url": "git+ssh://git@github.com:zo0r/react-native-push-notification.git" }, "dependencies": { - "@react-native-community/push-notification-ios": "^1.2.2" + "@react-native-community/push-notification-ios": "^1.3.0" }, "peerDependencies": { "react-native": ">=0.33" From 579ffb898acef1068306dadd1e17dfc84fcce197 Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Wed, 8 Jul 2020 17:22:21 +0300 Subject: [PATCH 09/18] reflecting userInfo working on both platforms in documentation and example --- README.md | 2 +- example/NotifService.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0e5e37208..9735c4205 100644 --- a/README.md +++ b/README.md @@ -319,9 +319,9 @@ PushNotification.localNotification({ /* iOS only properties */ alertAction: "view", // (optional) default: view category: "", // (optional) default: empty string - userInfo: {}, // (optional) default: {} (using null throws a JSON value '' error) /* iOS and Android properties */ + userInfo: {}, // (optional) default: {} (using null throws a JSON value '' error) title: "My Notification Title", // (optional) message: "My Notification Message", // (required) playSound: false, // (optional) default: true diff --git a/example/NotifService.js b/example/NotifService.js index ab9bac75d..22b5f1c69 100644 --- a/example/NotifService.js +++ b/example/NotifService.js @@ -79,9 +79,9 @@ export default class NotifService { /* iOS only properties */ alertAction: 'view', // (optional) default: view category: '', // (optional) default: empty string - userInfo: {}, // (optional) default: {} (using null throws a JSON value '' error) - + /* iOS and Android properties */ + userInfo: {}, // (optional) default: {} (using null throws a JSON value '' error) title: 'Scheduled Notification', // (optional) message: 'My Notification Message', // (required) playSound: !!soundName, // (optional) default: true From 3e191058cfedd0ed694203602abed0b36842e325 Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Wed, 8 Jul 2020 17:25:34 +0300 Subject: [PATCH 10/18] revert changes --- example/App.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/App.js b/example/App.js index d6bebc85c..a4c8458b0 100644 --- a/example/App.js +++ b/example/App.js @@ -108,7 +108,7 @@ export default class App extends Component { { - this.notif.getScheduledLocalNotifications().then(notifs => console.log(notifs)); + this.notif.getScheduledLocalNotifications(notifs => console.log(notifs)); }}> Console.Log Scheduled Local Notifications From 85952df014650b79d6aecc69b6f0b479d15a30d3 Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Wed, 8 Jul 2020 17:31:34 +0300 Subject: [PATCH 11/18] populate userInfo with id on both platforms --- index.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 3583018a0..bf8159c01 100644 --- a/index.js +++ b/index.js @@ -152,6 +152,12 @@ 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 @@ -161,12 +167,6 @@ 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 @@ -228,6 +228,12 @@ 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 @@ -235,12 +241,6 @@ Notifications.localNotificationSchedule = 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}; - } - const iosDetails = { fireDate: details.date.toISOString(), alertTitle: details.title, From 22d8c752fb247cc1248cf516910bc0c7537426a1 Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Wed, 8 Jul 2020 17:42:51 +0300 Subject: [PATCH 12/18] return notification id in onNotification method on iOS --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index bf8159c01..b8c18dfe0 100644 --- a/index.js +++ b/index.js @@ -344,12 +344,13 @@ Notifications._onNotification = function(data, isFromBackground = null) { if ( this.onNotification !== false ) { if ( Platform.OS === 'ios' ) { + const notifData = data.getData(); this.onNotification({ - id: data?.userInfo?.id, + id: notifData?.id, foreground: ! isFromBackground, userInteraction: isFromBackground, message: data.getMessage(), - data: data.getData(), + data: notifData, badge: data.getBadgeCount(), alert: data.getAlert(), sound: data.getSound(), From 9e8480328c02d84ec1dfa829dd17e01b5a9b70b3 Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Tue, 21 Jul 2020 10:42:48 +0300 Subject: [PATCH 13/18] align onNotification on both platforms --- README.md | 4 ++-- example/NotifService.js | 12 ++++++------ example/ios/Podfile.lock | 4 ++-- example/package.json | 2 +- index.js | 29 ++++++++--------------------- package.json | 2 +- 6 files changed, 20 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index a79e981d7..3bc4cade5 100644 --- a/README.md +++ b/README.md @@ -299,7 +299,6 @@ EXAMPLE: ```javascript PushNotification.localNotification({ /* Android Only Properties */ - id: 0, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID ticker: "My Notification Ticker", // (optional) showWhen: true, // (optional) default: true autoCancel: true, // (optional) default: true @@ -332,9 +331,10 @@ PushNotification.localNotification({ category: "", // (optional) default: empty string /* iOS and Android properties */ - userInfo: {}, // (optional) default: {} (using null throws a JSON value '' error) + 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) + 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) number: 10, // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero) diff --git a/example/NotifService.js b/example/NotifService.js index c1f3f92b1..eb8259704 100644 --- a/example/NotifService.js +++ b/example/NotifService.js @@ -24,7 +24,6 @@ export default class NotifService { this.lastId++; PushNotification.localNotification({ /* Android Only Properties */ - id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID ticker: 'My Notification Ticker', // (optional) autoCancel: true, // (optional) default: true largeIcon: 'ic_launcher', // (optional) default: "ic_launcher" @@ -39,15 +38,16 @@ export default class NotifService { ongoing: false, // (optional) set whether this is an "ongoing" notification actions: ['Yes', 'No'], // (Android only) See the doc for notification actions to know more invokeApp: true, // (optional) This enable click on actions to bring back the application to foreground or stay in background, default: true - + /* iOS only properties */ alertAction: 'view', // (optional) default: view category: '', // (optional) default: empty string - userInfo: {}, // (optional) default: {} (using null throws a JSON value '' error) - + /* iOS and Android properties */ + id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID title: 'Local Notification', // (optional) message: 'My Notification Message', // (required) + userInfo: { screen: 'home' }, // (optional) default: {} (using null throws a JSON value '' error) playSound: !!soundName, // (optional) default: true soundName: soundName ? 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) number: 10, // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero) @@ -60,7 +60,6 @@ export default class NotifService { date: new Date(Date.now() + 30 * 1000), // in 30 secs /* Android Only Properties */ - id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID ticker: 'My Notification Ticker', // (optional) autoCancel: true, // (optional) default: true largeIcon: 'ic_launcher', // (optional) default: "ic_launcher" @@ -81,9 +80,10 @@ export default class NotifService { category: '', // (optional) default: empty string /* iOS and Android properties */ - userInfo: {}, // (optional) default: {} (using null throws a JSON value '' error) + id: this.lastId, // (optional) Valid unique 32 bit integer specified as string. default: Autogenerated Unique ID title: 'Scheduled Notification', // (optional) message: 'My Notification Message', // (required) + userInfo: { sceen: "home" }, // (optional) default: {} (using null throws a JSON value '' error) playSound: !!soundName, // (optional) default: true number: 10, // (optional) Valid 32 bit integer specified as string. default: none (Cannot be zero) soundName: soundName ? 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) diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 2f44b3dad..1ef8335cd 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -292,7 +292,7 @@ PODS: - React-cxxreact (= 0.62.2) - React-jsi (= 0.62.2) - ReactCommon/callinvoker (= 0.62.2) - - RNCPushNotificationIOS (1.3.0): + - RNCPushNotificationIOS (1.4.0): - React - Yoga (1.14.0) - YogaKit (1.18.1): @@ -453,7 +453,7 @@ SPEC CHECKSUMS: React-RCTText: fae545b10cfdb3d247c36c56f61a94cfd6dba41d React-RCTVibration: 4356114dbcba4ce66991096e51a66e61eda51256 ReactCommon: ed4e11d27609d571e7eee8b65548efc191116eb3 - RNCPushNotificationIOS: d5fd66aed4e03c6491ca0c6111a03d4f6455ff6c + RNCPushNotificationIOS: dc1c0c6aa18a128df123598149f42e848d26a4ac Yoga: 3ebccbdd559724312790e7742142d062476b698e YogaKit: f782866e155069a2cca2517aafea43200b01fd5a diff --git a/example/package.json b/example/package.json index 091ea7bee..ca8632cae 100644 --- a/example/package.json +++ b/example/package.json @@ -11,7 +11,7 @@ "pod-install": "cd ios && pod install" }, "dependencies": { - "@react-native-community/push-notification-ios": "^1.3.0", + "@react-native-community/push-notification-ios": "^1.4.0", "react": "16.11.0", "react-native": "0.62.2", "react-native-push-notification": "git+https://git@github.com/zo0r/react-native-push-notification.git" diff --git a/index.js b/index.js index b8c18dfe0..1388711b4 100644 --- a/index.js +++ b/index.js @@ -190,10 +190,6 @@ Notifications.localNotification = function(details) { } } - if(!details.data && details.userInfo){ - details.data = details.userInfo; - } - if (details && typeof details.shortcutId === 'number') { if(isNaN(details.shortcutId)) { console.warn('NaN value has been passed as shortcutId'); @@ -281,10 +277,6 @@ Notifications.localNotificationSchedule = function(details) { details.shortcutId = '' + details.shortcutId; } } - - if(!details.data && details.userInfo){ - details.data = details.userInfo; - } if(details && Array.isArray(details.actions)) { details.actions = JSON.stringify(details.actions); @@ -352,25 +344,20 @@ Notifications._onNotification = function(data, isFromBackground = null) { message: data.getMessage(), data: notifData, badge: data.getBadgeCount(), - alert: data.getAlert(), - sound: data.getSound(), - fireDate: data._fireDate, + title: data.getTitle(), + soundName: data.getSound(), + fireDate: Date.parse(data._fireDate), finish: (res) => data.finish(res) }); } else { var notificationData = { foreground: ! isFromBackground, finish: () => {}, - ...data + ...data, + data: data?.userInfo, }; - - if ( typeof notificationData.data === 'string' ) { - try { - notificationData.data = JSON.parse(notificationData.data); - } catch(e) { - /* void */ - } - } + delete notificationData.userInfo; + delete notificationData.notificationId; this.onNotification(notificationData); } @@ -480,7 +467,7 @@ Notifications.getScheduledLocalNotifications = function(callback) { soundName: notif.soundName, repeatInterval: notif.repeatInterval, id: notif.userInfo?.id, - date: new Date(notif.fireDate), + date: new Date(notif.fireDate), number: notif?.applicationIconBadgeNumber, message: notif?.alertBody, title: notif?.alertTitle, diff --git a/package.json b/package.json index 7b4afe9c3..ad4c56cbd 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "url": "git+ssh://git@github.com:zo0r/react-native-push-notification.git" }, "dependencies": { - "@react-native-community/push-notification-ios": "^1.3.0" + "@react-native-community/push-notification-ios": "^1.4.0" }, "peerDependencies": { "react-native": ">=0.33" From 9cc416751ceea69c3c1396b07a0756fcd2d1e754 Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Mon, 27 Jul 2020 14:07:01 +0300 Subject: [PATCH 14/18] keep notification data from fcm while populating data userInfo --- index.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 1388711b4..249d26f56 100644 --- a/index.js +++ b/index.js @@ -354,7 +354,19 @@ Notifications._onNotification = function(data, isFromBackground = null) { foreground: ! isFromBackground, finish: () => {}, ...data, - data: data?.userInfo, + }; + + if ( typeof notificationData.data === 'string' ) { + try { + notificationData.data = JSON.parse(notificationData.data); + } catch(e) { + /* void */ + } + } + + notificationData.data = { + ...(notificationData.data ? notificationData.data : {}), + ...(notificationData.data ? notificationData.userInfo : {}) }; delete notificationData.userInfo; delete notificationData.notificationId; From d547687e47311291d5e903102064260048624983 Mon Sep 17 00:00:00 2001 From: Lukas Baranauskas Date: Mon, 27 Jul 2020 14:13:17 +0300 Subject: [PATCH 15/18] make sure data/userInfo is an object before spreading --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 249d26f56..59cfa7633 100644 --- a/index.js +++ b/index.js @@ -365,8 +365,8 @@ Notifications._onNotification = function(data, isFromBackground = null) { } notificationData.data = { - ...(notificationData.data ? notificationData.data : {}), - ...(notificationData.data ? notificationData.userInfo : {}) + ...(typeof notificationData.data === 'object' ? notificationData.data : {}), + ...(typeof notificationData.userInfo === 'object' ? notificationData.userInfo : {}) }; delete notificationData.userInfo; delete notificationData.notificationId; From 7589c4a382c1d4773616f4d1998f0e32329cbdfb Mon Sep 17 00:00:00 2001 From: Boris Tacyniak Date: Mon, 27 Jul 2020 22:14:54 +0200 Subject: [PATCH 16/18] Apply changes to popInitialNotification. --- index.js | 98 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 39 deletions(-) diff --git a/index.js b/index.js index 59cfa7633..49292b2f8 100644 --- a/index.js +++ b/index.js @@ -326,7 +326,11 @@ Notifications._onAction = function(notification) { this.onAction(notification); } -Notifications._onNotification = function(data, isFromBackground = null) { +Notifications._transformNotificationObject = function(data, isFromBackground = null) { + if(!data) { + return; + } + if ( isFromBackground === null ) { isFromBackground = ( data.foreground === false || @@ -334,45 +338,59 @@ Notifications._onNotification = function(data, isFromBackground = null) { ); } - if ( this.onNotification !== false ) { - if ( Platform.OS === 'ios' ) { - const notifData = data.getData(); - this.onNotification({ - id: notifData?.id, - foreground: ! isFromBackground, - userInteraction: isFromBackground, - message: data.getMessage(), - data: notifData, - badge: data.getBadgeCount(), - title: data.getTitle(), - soundName: data.getSound(), - fireDate: Date.parse(data._fireDate), - finish: (res) => data.finish(res) - }); - } else { - var notificationData = { - foreground: ! isFromBackground, - finish: () => {}, - ...data, - }; - - if ( typeof notificationData.data === 'string' ) { - try { - notificationData.data = JSON.parse(notificationData.data); - } catch(e) { - /* void */ - } + let _notification; + + if ( Platform.OS === 'ios' ) { + const notifData = data.getData(); + + _notification = { + id: notifData?.id, + foreground: !isFromBackground, + userInteraction: isFromBackground, + message: data.getMessage(), + data: notifData, + badge: data.getBadgeCount(), + title: data.getTitle(), + soundName: data.getSound(), + fireDate: Date.parse(data._fireDate), + finish: (res) => data.finish(res) + }; + } else { + _notification = { + foreground: ! isFromBackground, + finish: () => {}, + ...data, + }; + + if ( typeof _notification.data === 'string' ) { + try { + _notification.data = JSON.parse(_notification.data); + } catch(e) { + /* void */ } + } + + _notification.data = { + ...(typeof _notification.userInfo === 'object' ? _notification.userInfo : {}), + ...(typeof _notification.data === 'object' ? _notification.data : {}), + }; + + delete _notification.userInfo; + delete _notification.notificationId; + } - notificationData.data = { - ...(typeof notificationData.data === 'object' ? notificationData.data : {}), - ...(typeof notificationData.userInfo === 'object' ? notificationData.userInfo : {}) - }; - delete notificationData.userInfo; - delete notificationData.notificationId; + return _notification; +} + +Notifications._onNotification = function(data, initialNotification = false) { + if ( this.onNotification !== false ) { + let notification = data; - this.onNotification(notificationData); + if(!initialNotification) { + notification = this._transformNotificationObject(data); } + + this.onNotification(notification); } }; @@ -442,8 +460,10 @@ Notifications.getApplicationIconBadgeNumber = function() { }; Notifications.popInitialNotification = function(handler) { - this.callNative('getInitialNotification').then(function(result){ - handler(result); + this.callNative('getInitialNotification').then((result) => { + handler( + this._transformNotificationObject(result, true) + ); }); }; @@ -479,7 +499,7 @@ Notifications.getScheduledLocalNotifications = function(callback) { soundName: notif.soundName, repeatInterval: notif.repeatInterval, id: notif.userInfo?.id, - date: new Date(notif.fireDate), + date: new Date(notif.fireDate), number: notif?.applicationIconBadgeNumber, message: notif?.alertBody, title: notif?.alertTitle, From 271461ee5039de11ce24d07285ff470d622f88ae Mon Sep 17 00:00:00 2001 From: Boris Tacyniak Date: Mon, 27 Jul 2020 22:40:19 +0200 Subject: [PATCH 17/18] fix popInitialNotification and null pointer Fix a case where popInitialNotification is not triggered. Fix a null pointer exception. --- index.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 49292b2f8..dd1d42107 100644 --- a/index.js +++ b/index.js @@ -95,27 +95,30 @@ Notifications.configure = function(options) { this.isLoaded = true; } - const handlePopInitialNotification = function(state) { + const handlePopInitialNotification = (state) => { if('active' !== state) { return; } if (options.popInitialNotification === undefined || options.popInitialNotification === true) { this.popInitialNotification(function(firstNotification) { - if ( firstNotification !== null ) { - if(false === firstNotification.userInteraction || this.idInitialNotification === firstNotification.id) { - return; - } - - this.idInitialNotification = firstNotification.id; - this._onNotification(firstNotification, true); + if (!firstNotification) { + return; } + + if(false === firstNotification.userInteraction || this.idInitialNotification === firstNotification.id) { + return; + } + + this.idInitialNotification = firstNotification.id; + this._onNotification(firstNotification, true); }.bind(this)); } } AppState.addEventListener('change', handlePopInitialNotification.bind(this)); - handlePopInitialNotification(); + + handlePopInitialNotification(AppState.currentState); if ( options.requestPermissions !== false ) { this._requestPermissions(); @@ -499,7 +502,7 @@ Notifications.getScheduledLocalNotifications = function(callback) { soundName: notif.soundName, repeatInterval: notif.repeatInterval, id: notif.userInfo?.id, - date: new Date(notif.fireDate), + date: new Date(notif.fireDate), number: notif?.applicationIconBadgeNumber, message: notif?.alertBody, title: notif?.alertTitle, From 762f4c09cf68557d375d462f72cc758e5df229e6 Mon Sep 17 00:00:00 2001 From: Boris Tacyniak Date: Fri, 31 Jul 2020 16:49:05 +0200 Subject: [PATCH 18/18] Fix behaviour of popInitialNotification and onNotification. Fix foreground value. --- .../modules/RNPushNotification.java | 2 -- .../modules/RNPushNotificationHelper.java | 5 +++-- .../modules/RNPushNotificationPublisher.java | 2 +- index.js | 11 ++++++----- 4 files changed, 10 insertions(+), 10 deletions(-) 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 c268cd452..10d5835ef 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotification.java @@ -89,8 +89,6 @@ private Bundle getBundleFromIntent(Intent intent) { public void onNewIntent(Intent intent) { Bundle bundle = this.getBundleFromIntent(intent); if (bundle != null) { - bundle.putBoolean("foreground", false); - intent.putExtra("notification", bundle); mJsDelivery.notifyNotification(bundle); } } 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 ecd61b03e..22c2622b1 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationHelper.java @@ -422,6 +422,7 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB Intent intent = new Intent(context, intentClass); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + bundle.putBoolean("foreground", this.isApplicationInForeground()); bundle.putBoolean("userInteraction", true); intent.putExtra("notification", bundle); @@ -583,7 +584,7 @@ public void sendToNotificationCentreWithPicture(Bundle bundle, Bitmap largeIconB editor.apply(); } - if (!(this.isApplicationInForeground(context) && bundle.getBoolean("ignoreInForeground"))) { + if (!(this.isApplicationInForeground() && bundle.getBoolean("ignoreInForeground"))) { Notification info = notification.build(); info.defaults |= Notification.DEFAULT_LIGHTS; @@ -938,7 +939,7 @@ private void checkOrCreateChannel(NotificationManager manager, String channel_id } } - public boolean isApplicationInForeground(Context context) { + public boolean isApplicationInForeground() { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List processInfos = activityManager.getRunningAppProcesses(); if (processInfos != null) { diff --git a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationPublisher.java b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationPublisher.java index cb3c6d33f..613e23eee 100644 --- a/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationPublisher.java +++ b/android/src/main/java/com/dieam/reactnativepushnotification/modules/RNPushNotificationPublisher.java @@ -40,7 +40,7 @@ private void handleLocalNotification(Context context, Bundle bundle) { Application applicationContext = (Application) context.getApplicationContext(); RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext); - boolean isForeground = pushNotificationHelper.isApplicationInForeground(applicationContext); + boolean isForeground = pushNotificationHelper.isApplicationInForeground(); Log.v(LOG_TAG, "sendNotification: " + bundle); diff --git a/index.js b/index.js index dd1d42107..46e1c6fcc 100644 --- a/index.js +++ b/index.js @@ -20,7 +20,7 @@ var Notifications = { onAction: false, onRemoteFetch: false, isLoaded: false, - idInitialNotification: null, + isPopInitialNotification: false, isPermissionsRequestPending: false, @@ -102,15 +102,16 @@ Notifications.configure = function(options) { if (options.popInitialNotification === undefined || options.popInitialNotification === true) { this.popInitialNotification(function(firstNotification) { - if (!firstNotification) { + if(this.isPopInitialNotification) { return; } - - if(false === firstNotification.userInteraction || this.idInitialNotification === firstNotification.id) { + + this.isPopInitialNotification = true; + + if (!firstNotification || false === firstNotification.userInteraction) { return; } - this.idInitialNotification = firstNotification.id; this._onNotification(firstNotification, true); }.bind(this)); }