From 5494037f78fcb106398c6646b8ac2381492915c1 Mon Sep 17 00:00:00 2001 From: Gleidson Daniel Silva Date: Thu, 7 Apr 2022 16:14:04 -0300 Subject: [PATCH] Chore: Upgrade react-native-notifications to 4.2.4 (#3958) Co-authored-by: Diego Mello --- android/app/build.gradle | 4 +- .../rocket/reactnative/AdditionalModules.java | 4 +- android/settings.gradle | 2 - app/definitions/INotification.ts | 24 +++--- app/notifications/push/index.ts | 5 +- app/notifications/push/push.ios.ts | 63 --------------- app/notifications/push/push.ts | 76 +++++++++++++++--- ios/Podfile.lock | 6 +- package.json | 2 +- .../react-native-notifications+2.1.7.patch | 28 ------- .../react-native-notifications+4.2.4.patch | 79 +++++++++++++++++++ react-native.config.js | 5 -- yarn.lock | 16 +--- 13 files changed, 169 insertions(+), 145 deletions(-) delete mode 100644 app/notifications/push/push.ios.ts delete mode 100644 patches/react-native-notifications+2.1.7.patch create mode 100644 patches/react-native-notifications+4.2.4.patch diff --git a/android/app/build.gradle b/android/app/build.gradle index bedf03d3..660ec885 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -277,14 +277,14 @@ android { dependencies { addUnimodulesDependencies() implementation project(':@react-native-community_viewpager') - playImplementation project(':reactnativenotifications') + playImplementation project(':react-native-notifications') + playImplementation 'com.google.firebase:firebase-core:16.0.0' playImplementation project(':@react-native-firebase_app') playImplementation project(':@react-native-firebase_analytics') playImplementation project(':@react-native-firebase_crashlytics') implementation fileTree(dir: "libs", include: ["*.jar"]) //noinspection GradleDynamicVersion implementation "com.facebook.react:react-native:+" // From node_modules - playImplementation "com.google.firebase:firebase-messaging:18.0.0" implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0" debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") { exclude group:'com.facebook.fbjni' diff --git a/android/app/src/play/java/chat/rocket/reactnative/AdditionalModules.java b/android/app/src/play/java/chat/rocket/reactnative/AdditionalModules.java index 2b3dd08c..5a32de49 100644 --- a/android/app/src/play/java/chat/rocket/reactnative/AdditionalModules.java +++ b/android/app/src/play/java/chat/rocket/reactnative/AdditionalModules.java @@ -3,7 +3,6 @@ package chat.rocket.reactnative; import android.app.Application; import com.facebook.react.ReactPackage; -import com.wix.reactnativenotifications.RNNotificationsPackage; import java.util.Arrays; import java.util.List; @@ -17,8 +16,7 @@ public class AdditionalModules { return Arrays.asList( new ReactNativeFirebaseAnalyticsPackage(), new ReactNativeFirebaseAppPackage(), - new ReactNativeFirebaseCrashlyticsPackage(), - new RNNotificationsPackage(application) + new ReactNativeFirebaseCrashlyticsPackage() ); } } diff --git a/android/settings.gradle b/android/settings.gradle index 98573a99..85d0fc08 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -2,8 +2,6 @@ apply from: '../node_modules/react-native-unimodules/gradle.groovy' includeUnimodulesProjects() rootProject.name = 'RocketChatRN' -include ':reactnativenotifications' -project(':reactnativenotifications').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-notifications/android/app') include ':@react-native-community_viewpager' project(':@react-native-community_viewpager').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/viewpager/android') include ':@react-native-firebase_app' diff --git a/app/definitions/INotification.ts b/app/definitions/INotification.ts index 4974b0c9..f387786a 100644 --- a/app/definitions/INotification.ts +++ b/app/definitions/INotification.ts @@ -1,13 +1,15 @@ export interface INotification { - message: string; - style: string; - ejson: string; - collapse_key: string; - notId: string; - msgcnt: string; - title: string; - from: string; - image: string; - soundname: string; - getData: () => INotification; + payload: { + message: string; + style: string; + ejson: string; + collapse_key: string; + notId: string; + msgcnt: string; + title: string; + from: string; + image: string; + soundname: string; + }; + identifier: string; } diff --git a/app/notifications/push/index.ts b/app/notifications/push/index.ts index 6fced7ed..0928c419 100644 --- a/app/notifications/push/index.ts +++ b/app/notifications/push/index.ts @@ -17,9 +17,9 @@ interface IEjson { } export const onNotification = (push: INotification): void => { - if (push) { + if (push.payload) { try { - const notification = push?.getData(); + const notification = push.payload; const { rid, name, sender, type, host, messageType, messageId }: IEjson = EJSON.parse(notification.ejson); const types: Record = { @@ -40,7 +40,6 @@ export const onNotification = (push: INotification): void => { path: `${types[type]}/${roomName}`, isCall: messageType === 'jitsi_call_started' }; - // TODO REDUX MIGRATION TO TS store.dispatch(deepLinkingOpen(params)); } catch (e) { console.warn(e); diff --git a/app/notifications/push/push.ios.ts b/app/notifications/push/push.ios.ts deleted file mode 100644 index 00b107b0..00000000 --- a/app/notifications/push/push.ios.ts +++ /dev/null @@ -1,63 +0,0 @@ -// @ts-ignore -// TODO BUMP LIB VERSION -import NotificationsIOS, { NotificationAction, NotificationCategory, Notification } from 'react-native-notifications'; - -import { store as reduxStore } from '../../lib/store/auxStore'; -import I18n from '../../i18n'; -import { INotification } from '../../definitions/INotification'; - -class PushNotification { - onNotification: (notification: Notification) => void; - deviceToken: string; - - constructor() { - this.onNotification = () => {}; - this.deviceToken = ''; - - NotificationsIOS.addEventListener('remoteNotificationsRegistered', (deviceToken: string) => { - this.deviceToken = deviceToken; - }); - - NotificationsIOS.addEventListener('notificationOpened', (notification: Notification, completion: () => void) => { - // TODO REDUX MIGRATION TO TS - const { background } = reduxStore.getState().app; - if (background) { - this.onNotification(notification); - } - completion(); - }); - - const actions = [ - new NotificationCategory({ - identifier: 'MESSAGE', - actions: [ - new NotificationAction({ - activationMode: 'background', - title: I18n.t('Reply'), - textInput: { - buttonTitle: I18n.t('Reply'), - placeholder: I18n.t('Type_message') - }, - identifier: 'REPLY_ACTION' - }) - ] - }) - ]; - NotificationsIOS.requestPermissions(actions); - } - - getDeviceToken() { - return this.deviceToken; - } - - setBadgeCount = (count = 0) => { - NotificationsIOS.setBadgesCount(count); - }; - - async configure(onNotification: (notification: INotification) => void) { - this.onNotification = onNotification; - const initial = await NotificationsIOS.getInitialNotification(); - return Promise.resolve(initial); - } -} -export default new PushNotification(); diff --git a/app/notifications/push/push.ts b/app/notifications/push/push.ts index 11eccbcb..e4bec45f 100644 --- a/app/notifications/push/push.ts +++ b/app/notifications/push/push.ts @@ -1,35 +1,87 @@ -// @ts-ignore -// TODO BUMP LIB VERSION -import { NotificationsAndroid, PendingNotifications, Notification } from 'react-native-notifications'; +import { + Notifications, + Registered, + RegistrationError, + NotificationCompletion, + Notification, + NotificationAction, + NotificationCategory +} from 'react-native-notifications'; import { INotification } from '../../definitions/INotification'; +import { isIOS } from '../../utils/deviceInfo'; +import { store as reduxStore } from '../../lib/store/auxStore'; +import I18n from '../../i18n'; class PushNotification { - onNotification: (notification: Notification) => void; + onNotification: (notification: any) => void; deviceToken: string; constructor() { this.onNotification = () => {}; this.deviceToken = ''; + if (isIOS) { + // init + Notifications.ios.registerRemoteNotifications(); - NotificationsAndroid.setRegistrationTokenUpdateListener((deviceToken: string) => { - this.deviceToken = deviceToken; + // setCategories + const notificationAction = new NotificationAction('REPLY_ACTION', 'background', I18n.t('Reply'), true, { + buttonTitle: I18n.t('Reply'), + placeholder: I18n.t('Type_message') + }); + const notificationCategory = new NotificationCategory('MESSAGE', [notificationAction]); + Notifications.setCategories([notificationCategory]); + } else { + // init + Notifications.android.registerRemoteNotifications(); + } + + Notifications.events().registerRemoteNotificationsRegistered((event: Registered) => { + this.deviceToken = event.deviceToken; }); - NotificationsAndroid.setNotificationOpenedListener((notification: Notification) => { - this.onNotification(notification); + Notifications.events().registerRemoteNotificationsRegistrationFailed((event: RegistrationError) => { + // TODO: Handle error + console.log(event); }); + + Notifications.events().registerNotificationReceivedForeground( + (notification: Notification, completion: (response: NotificationCompletion) => void) => { + completion({ alert: true, sound: true, badge: false }); + } + ); + + Notifications.events().registerNotificationOpened((notification: Notification, completion: () => void) => { + if (isIOS) { + const { background } = reduxStore.getState().app; + if (background) { + this.onNotification(notification); + } + } else { + this.onNotification(notification); + } + completion(); + }); + + Notifications.events().registerNotificationReceivedBackground( + (notification: Notification, completion: (response: any) => void) => { + completion({ alert: true, sound: true, badge: false }); + } + ); } getDeviceToken() { return this.deviceToken; } - setBadgeCount = (_?: number) => {}; + setBadgeCount = (count?: number) => { + if (isIOS && count) { + Notifications.ios.setBadgeCount(count); + } + }; - configure(onNotification: (notification: INotification) => void) { + configure(onNotification: (notification: INotification) => void): Promise { this.onNotification = onNotification; - NotificationsAndroid.refreshToken(); - return PendingNotifications.getInitialNotification(); + return Notifications.getInitialNotification(); } } diff --git a/ios/Podfile.lock b/ios/Podfile.lock index 737b3567..b0240783 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -415,8 +415,8 @@ PODS: - React-Core - react-native-netinfo (6.0.0): - React-Core - - react-native-notifications (2.1.7): - - React + - react-native-notifications (4.2.4): + - React-Core - react-native-orientation-locker (1.1.8): - React - react-native-restart (0.0.22): @@ -1004,7 +1004,7 @@ SPEC CHECKSUMS: react-native-jitsi-meet: 3e3ac5d0445091154119f94342efd55c8b1124ce react-native-mmkv-storage: 88bcb10bbe85a8122061d17d03abcc64a02fe1c9 react-native-netinfo: e849fc21ca2f4128a5726c801a82fc6f4a6db50d - react-native-notifications: ee8fd739853e72694f3af8b374c8ccb106b7b227 + react-native-notifications: 3de8ef9cd800e5db0225d9aa46b228d2b94ce51e react-native-orientation-locker: f0ca1a8e5031dab6b74bfb4ab33a17ed2c2fcb0d react-native-restart: 733a51ad137f15b0f8dc34c4082e55af7da00979 react-native-safe-area-context: f0906bf8bc9835ac9a9d3f97e8bde2a997d8da79 diff --git a/package.json b/package.json index b2ca7593..b25456a9 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,7 @@ "react-native-mmkv-storage": "0.6.12", "react-native-modal": "11.10.0", "react-native-navigation-bar-color": "2.0.1", - "react-native-notifications": "2.1.7", + "react-native-notifications": "^4.2.4", "react-native-notifier": "1.6.1", "react-native-orientation-locker": "1.1.8", "react-native-picker-select": "^8.0.4", diff --git a/patches/react-native-notifications+2.1.7.patch b/patches/react-native-notifications+2.1.7.patch deleted file mode 100644 index d6784c82..00000000 --- a/patches/react-native-notifications+2.1.7.patch +++ /dev/null @@ -1,28 +0,0 @@ -diff --git a/node_modules/react-native-notifications/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java b/node_modules/react-native-notifications/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java -index 524ff07..70f22d5 100644 ---- a/node_modules/react-native-notifications/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java -+++ b/node_modules/react-native-notifications/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java -@@ -31,7 +31,7 @@ public class PushNotification implements IPushNotification { - final protected AppLifecycleFacade mAppLifecycleFacade; - final protected AppLaunchHelper mAppLaunchHelper; - final protected JsIOHelper mJsIOHelper; -- final protected PushNotificationProps mNotificationProps; -+ protected PushNotificationProps mNotificationProps; - final protected AppVisibilityListener mAppVisibilityListener = new AppVisibilityListener() { - @Override - public void onAppVisible() { -diff --git a/node_modules/react-native-notifications/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java b/node_modules/react-native-notifications/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java -index f9c858b..94ea188 100644 ---- a/node_modules/react-native-notifications/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java -+++ b/node_modules/react-native-notifications/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java -@@ -2,8 +2,8 @@ - package com.wix.reactnativenotifications; - - import android.content.Context; --import android.support.annotation.NonNull; --import android.support.v4.app.NotificationManagerCompat; -+import androidx.annotation.NonNull; -+import androidx.core.app.NotificationManagerCompat; - - public abstract class NotificationManagerCompatFacade { - public static NotificationManagerCompat from(@NonNull Context context) { diff --git a/patches/react-native-notifications+4.2.4.patch b/patches/react-native-notifications+4.2.4.patch new file mode 100644 index 00000000..e2d84d88 --- /dev/null +++ b/patches/react-native-notifications+4.2.4.patch @@ -0,0 +1,79 @@ +diff --git a/node_modules/react-native-notifications/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java b/node_modules/react-native-notifications/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java +index f9c858b..94ea188 100644 +--- a/node_modules/react-native-notifications/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java ++++ b/node_modules/react-native-notifications/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java +@@ -2,8 +2,8 @@ + package com.wix.reactnativenotifications; + + import android.content.Context; +-import android.support.annotation.NonNull; +-import android.support.v4.app.NotificationManagerCompat; ++import androidx.annotation.NonNull; ++import androidx.core.app.NotificationManagerCompat; + + public abstract class NotificationManagerCompatFacade { + public static NotificationManagerCompat from(@NonNull Context context) { +diff --git a/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java b/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java +index b93f762..c2b736a 100644 +--- a/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java ++++ b/node_modules/react-native-notifications/lib/android/app/src/main/java/com/wix/reactnativenotifications/core/notification/PushNotification.java +@@ -28,7 +28,7 @@ public class PushNotification implements IPushNotification { + final protected AppLifecycleFacade mAppLifecycleFacade; + final protected AppLaunchHelper mAppLaunchHelper; + final protected JsIOHelper mJsIOHelper; +- final protected PushNotificationProps mNotificationProps; ++ protected PushNotificationProps mNotificationProps; + final protected AppVisibilityListener mAppVisibilityListener = new AppVisibilityListener() { + @Override + public void onAppVisible() { +diff --git a/node_modules/react-native-notifications/lib/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java b/node_modules/react-native-notifications/lib/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java +index f9c858b..94ea188 100644 +--- a/node_modules/react-native-notifications/lib/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java ++++ b/node_modules/react-native-notifications/lib/android/app/src/reactNative59/java/com/wix/reactnativenotifications/NotificationManagerCompatFacade.java +@@ -2,8 +2,8 @@ + package com.wix.reactnativenotifications; + + import android.content.Context; +-import android.support.annotation.NonNull; +-import android.support.v4.app.NotificationManagerCompat; ++import androidx.annotation.NonNull; ++import androidx.core.app.NotificationManagerCompat; + + public abstract class NotificationManagerCompatFacade { + public static NotificationManagerCompat from(@NonNull Context context) { +diff --git a/node_modules/react-native-notifications/lib/ios/RNNotificationCenter.m b/node_modules/react-native-notifications/lib/ios/RNNotificationCenter.m +index 4b33656..36aaa47 100644 +--- a/node_modules/react-native-notifications/lib/ios/RNNotificationCenter.m ++++ b/node_modules/react-native-notifications/lib/ios/RNNotificationCenter.m +@@ -29,18 +29,20 @@ - (void)requestPermissions:(NSDictionary *)options { + authOptions = authOptions | UNAuthorizationOptionAnnouncement; + } + } ++ if(![[[NSBundle mainBundle] bundlePath] hasSuffix:@".appex"]){ ++ [UNUserNotificationCenter.currentNotificationCenter requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { ++ if (!error && granted) { ++ [UNUserNotificationCenter.currentNotificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { ++ if (settings.authorizationStatus == UNAuthorizationStatusAuthorized || settings.authorizationStatus == UNAuthorizationStatusProvisional) { ++ dispatch_async(dispatch_get_main_queue(), ^{ ++ [[UIApplication sharedApplication] registerForRemoteNotifications]; ++ }); ++ } ++ }]; ++ } ++ }]; ++ } + +- [UNUserNotificationCenter.currentNotificationCenter requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) { +- if (!error && granted) { +- [UNUserNotificationCenter.currentNotificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { +- if (settings.authorizationStatus == UNAuthorizationStatusAuthorized || settings.authorizationStatus == UNAuthorizationStatusProvisional) { +- dispatch_async(dispatch_get_main_queue(), ^{ +- [[UIApplication sharedApplication] registerForRemoteNotifications]; +- }); +- } +- }]; +- } +- }]; + } + + - (void)setCategories:(NSArray *)json { diff --git a/react-native.config.js b/react-native.config.js index 6f2362f4..9e78b41a 100644 --- a/react-native.config.js +++ b/react-native.config.js @@ -1,10 +1,5 @@ module.exports = { dependencies: { - 'react-native-notifications': { - platforms: { - android: null - } - }, '@react-native-firebase/app': { platforms: { android: null diff --git a/yarn.lock b/yarn.lock index b7545bce..aadfc41f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -14836,13 +14836,10 @@ react-native-navigation-bar-color@2.0.1: resolved "https://registry.yarnpkg.com/react-native-navigation-bar-color/-/react-native-navigation-bar-color-2.0.1.tgz#ee2be25cc37105f7da355717b0a9a32c9c059ae6" integrity sha512-1kE/oxWt+HYjRxdZdvke9tJ365xaee5n3+euOQA1En8zQuSbOxiE4SYEGM7TeaWnmLJ0l37mRnPHaB2H4mGh0A== -react-native-notifications@2.1.7: - version "2.1.7" - resolved "https://registry.yarnpkg.com/react-native-notifications/-/react-native-notifications-2.1.7.tgz#ef836f6f8c12bdc5103598035590347811426a70" - integrity sha512-m9jfpobP1BpqG2w6pN2kKzKJt2bqrLIRS3QAuH0MU0dpRwib/4ehaAUKOiFj+xVCtuKHALpceb97FHuA0i4dkw== - dependencies: - core-js "^1.0.0" - uuid "^2.0.3" +react-native-notifications@^4.2.4: + version "4.2.4" + resolved "https://registry.yarnpkg.com/react-native-notifications/-/react-native-notifications-4.2.4.tgz#0d686eb1576d3d9cb73dd9db1ee4f212e00f7d89" + integrity sha512-ffToxERa2bRUsXShCO19yXY6c6l4Esq7MqRKAb4mPSn+T428X7Je7WYvWOIOVw/BMGJ3R0lPrZk52vDpoYqanw== react-native-notifier@1.6.1: version "1.6.1" @@ -17683,11 +17680,6 @@ utils-merge@1.0.1: resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" integrity sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM= -uuid@^2.0.3: - version "2.0.3" - resolved "https://registry.yarnpkg.com/uuid/-/uuid-2.0.3.tgz#67e2e863797215530dff318e5bf9dcebfd47b21a" - integrity sha1-Z+LoY3lyFVMN/zGOW/nc6/1Hsho= - uuid@^3.0.1, uuid@^3.3.2, uuid@^3.4.0: version "3.4.0" resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"