From fd74ee344d26b478e571ee4fcf5b596ee8486f94 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Tue, 4 Jan 2022 16:25:11 -0300 Subject: [PATCH 1/7] wip: init --- app/notifications/push/{index.js => index.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/notifications/push/{index.js => index.ts} (100%) diff --git a/app/notifications/push/index.js b/app/notifications/push/index.ts similarity index 100% rename from app/notifications/push/index.js rename to app/notifications/push/index.ts From b104e7a8a7b79f307159fcd339f2d26b2a3b80d4 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Wed, 5 Jan 2022 18:19:21 -0300 Subject: [PATCH 2/7] add types for all push functions and interfaces --- app/notifications/push/index.ts | 90 ++++++++++++------- app/notifications/push/push.android.js | 32 ------- .../push/{push.ios.js => push.ios.ts} | 54 +++++------ app/notifications/push/push.ts | 36 ++++++++ package.json | 1 + yarn.lock | 5 ++ 6 files changed, 128 insertions(+), 90 deletions(-) delete mode 100644 app/notifications/push/push.android.js rename app/notifications/push/{push.ios.js => push.ios.ts} (50%) create mode 100644 app/notifications/push/push.ts diff --git a/app/notifications/push/index.ts b/app/notifications/push/index.ts index 074e22bf38..4930c893ac 100644 --- a/app/notifications/push/index.ts +++ b/app/notifications/push/index.ts @@ -5,46 +5,72 @@ import { deepLinkingOpen } from '../../actions/deepLinking'; import { isFDroidBuild } from '../../constants/environment'; import PushNotification from './push'; -export const onNotification = notification => { - if (notification) { - const data = notification.getData(); - if (data) { - try { - const { rid, name, sender, type, host, messageType, messageId } = EJSON.parse(data.ejson); +export interface INotification { + message: string; + style: string; + ejson: string; + collapse_key: string; + notId: string; + msgcnt: string; + title: string; + from: string; + image: string; + soundname: string; + // only for info + 'google.delivered_priority': string; + 'google.original_priority': string; + 'google.message_id': string; + 'google.sent_time': number; + 'google.c.sender.id': string; + 'google.ttl': number; +} + +interface IEjson { + rid: string; + name: string; + sender: { username: string; name: string }; + type: string; + host: string; + messageType: string; + messageId: string; +} - const types = { - c: 'channel', - d: 'direct', - p: 'group', - l: 'channels' - }; - let roomName = type === 'd' ? sender.username : name; - if (type === 'l') { - roomName = sender.name; - } +export const onNotification = (notification: INotification): void => { + if (notification) { + try { + const { rid, name, sender, type, host, messageType, messageId }: IEjson = EJSON.parse(notification.ejson); - const params = { - host, - rid, - messageId, - path: `${types[type]}/${roomName}`, - isCall: messageType === 'jitsi_call_started' - }; - store.dispatch(deepLinkingOpen(params)); - } catch (e) { - console.warn(e); + const types: Record = { + c: 'channel', + d: 'direct', + p: 'group', + l: 'channels' + }; + let roomName = type === 'd' ? sender.username : name; + if (type === 'l') { + roomName = sender.name; } + + const params = { + host, + rid, + messageId, + path: `${types[type]}/${roomName}`, + isCall: messageType === 'jitsi_call_started' + }; + // TODO REDUX MIGRATION TO TS + store.dispatch(deepLinkingOpen(params)); + } catch (e) { + console.warn(e); } } }; -export const getDeviceToken = () => PushNotification.getDeviceToken(); -export const setBadgeCount = count => PushNotification.setBadgeCount(count); -export const initializePushNotifications = () => { +export const getDeviceToken = (): string => PushNotification.getDeviceToken(); +export const setBadgeCount = (count?: number): void => PushNotification.setBadgeCount(count); +export const initializePushNotifications = (): void => { if (!isFDroidBuild) { setBadgeCount(); - return PushNotification.configure({ - onNotification - }); + return PushNotification.configure(onNotification); } }; diff --git a/app/notifications/push/push.android.js b/app/notifications/push/push.android.js deleted file mode 100644 index 51e767ad37..0000000000 --- a/app/notifications/push/push.android.js +++ /dev/null @@ -1,32 +0,0 @@ -import { NotificationsAndroid, PendingNotifications } from 'react-native-notifications'; - -class PushNotification { - constructor() { - this.onRegister = null; - this.onNotification = null; - this.deviceToken = null; - - NotificationsAndroid.setRegistrationTokenUpdateListener(deviceToken => { - this.deviceToken = deviceToken; - }); - - NotificationsAndroid.setNotificationOpenedListener(notification => { - this.onNotification(notification); - }); - } - - getDeviceToken() { - return this.deviceToken; - } - - setBadgeCount = () => {}; - - configure(params) { - this.onRegister = params.onRegister; - this.onNotification = params.onNotification; - NotificationsAndroid.refreshToken(); - return PendingNotifications.getInitialNotification(); - } -} - -export default new PushNotification(); diff --git a/app/notifications/push/push.ios.js b/app/notifications/push/push.ios.ts similarity index 50% rename from app/notifications/push/push.ios.js rename to app/notifications/push/push.ios.ts index 068be4ee90..9a643cee2d 100644 --- a/app/notifications/push/push.ios.js +++ b/app/notifications/push/push.ios.ts @@ -1,43 +1,48 @@ -import NotificationsIOS, { NotificationAction, NotificationCategory } from 'react-native-notifications'; +// @ts-ignore +// TODO BUMP LIB VERSION +import NotificationsIOS, { NotificationAction, NotificationCategory, Notification } from 'react-native-notifications'; import reduxStore from '../../lib/createStore'; import I18n from '../../i18n'; - -const replyAction = new NotificationAction({ - activationMode: 'background', - title: I18n.t('Reply'), - textInput: { - buttonTitle: I18n.t('Reply'), - placeholder: I18n.t('Type_message') - }, - identifier: 'REPLY_ACTION' -}); +import { INotification } from '.'; class PushNotification { + onNotification: (notification: Notification) => void; + deviceToken: string; + constructor() { - this.onRegister = null; - this.onNotification = null; - this.deviceToken = null; + this.onNotification = () => {}; + this.deviceToken = ''; - NotificationsIOS.addEventListener('remoteNotificationsRegistered', deviceToken => { + NotificationsIOS.addEventListener('remoteNotificationsRegistered', (deviceToken: string) => { this.deviceToken = deviceToken; }); - NotificationsIOS.addEventListener('notificationOpened', (notification, completion) => { + NotificationsIOS.addEventListener('notificationOpened', (notification: Notification, completion: () => void) => { + // TODO REDUX MIGRATION TO TS const { background } = reduxStore.getState().app; if (background) { - this.onNotification(notification); + this.onNotification(notification?.getData()); } completion(); }); - const actions = []; - actions.push( + const actions = [ new NotificationCategory({ identifier: 'MESSAGE', - actions: [replyAction] + 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); } @@ -49,12 +54,9 @@ class PushNotification { NotificationsIOS.setBadgesCount(count); }; - async configure(params) { - this.onRegister = params.onRegister; - this.onNotification = params.onNotification; - + async configure(onNotification: (notification: INotification) => void) { + this.onNotification = onNotification; const initial = await NotificationsIOS.getInitialNotification(); - // NotificationsIOS.consumeBackgroundQueue(); return Promise.resolve(initial); } } diff --git a/app/notifications/push/push.ts b/app/notifications/push/push.ts new file mode 100644 index 0000000000..f708381a67 --- /dev/null +++ b/app/notifications/push/push.ts @@ -0,0 +1,36 @@ +// @ts-ignore +// TODO BUMP LIB VERSION +import { NotificationsAndroid, PendingNotifications, Notification } from 'react-native-notifications'; + +import { INotification } from '.'; + +class PushNotification { + onNotification: (notification: Notification) => void; + deviceToken: string; + constructor() { + this.onNotification = () => {}; + this.deviceToken = ''; + + NotificationsAndroid.setRegistrationTokenUpdateListener((deviceToken: string) => { + this.deviceToken = deviceToken; + }); + + NotificationsAndroid.setNotificationOpenedListener((notification: Notification) => { + this.onNotification(notification?.getData()); + }); + } + + getDeviceToken() { + return this.deviceToken; + } + + setBadgeCount = (_?: number) => {}; + + configure(onNotification: (notification: INotification) => void) { + this.onNotification = onNotification; + NotificationsAndroid.refreshToken(); + return PendingNotifications.getInitialNotification(); + } +} + +export default new PushNotification(); diff --git a/package.json b/package.json index f807f61661..31e05a9fbd 100644 --- a/package.json +++ b/package.json @@ -144,6 +144,7 @@ "@rocket.chat/eslint-config": "^0.4.0", "@storybook/addon-storyshots": "5.3.21", "@storybook/react-native": "5.3.25", + "@types/ejson": "^2.1.3", "@types/jest": "^26.0.24", "@types/lodash": "^4.14.171", "@types/react": "^17.0.14", diff --git a/yarn.lock b/yarn.lock index b4ca2beb5c..f7f3a62a3e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4198,6 +4198,11 @@ resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0" integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ== +"@types/ejson@^2.1.3": + version "2.1.3" + resolved "https://registry.yarnpkg.com/@types/ejson/-/ejson-2.1.3.tgz#29a0028db4180b589b62eb2b6b9367c65b0c1864" + integrity sha512-Sd+XISmDWOypfDcsKeQkhykSMgYVAWJxhf7f0ySvfy2tYo+im26M/6FfqjCEiPSDAEugiuZKtA+wWeANKueWIg== + "@types/events@*": version "3.0.0" resolved "https://registry.yarnpkg.com/@types/events/-/events-3.0.0.tgz#2862f3f58a9a7f7c3e78d79f130dd4d71c25c2a7" From 91fc0ec23c8189562ff147a06cdf04e458e76980 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Wed, 5 Jan 2022 18:27:44 -0300 Subject: [PATCH 3/7] fix promise return --- app/notifications/push/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/notifications/push/index.ts b/app/notifications/push/index.ts index 4930c893ac..6c4f367eb4 100644 --- a/app/notifications/push/index.ts +++ b/app/notifications/push/index.ts @@ -68,7 +68,7 @@ export const onNotification = (notification: INotification): void => { export const getDeviceToken = (): string => PushNotification.getDeviceToken(); export const setBadgeCount = (count?: number): void => PushNotification.setBadgeCount(count); -export const initializePushNotifications = (): void => { +export const initializePushNotifications = (): Promise | undefined => { if (!isFDroidBuild) { setBadgeCount(); return PushNotification.configure(onNotification); From 4cece39ed64ab5f5220ba55a8c40caf67639c9aa Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Fri, 7 Jan 2022 11:31:05 -0300 Subject: [PATCH 4/7] chore: migrate INotification to definitions folder and create index --- app/definitions/INotification.ts | 12 ++++++++++++ app/definitions/index.ts | 7 +++++++ 2 files changed, 19 insertions(+) create mode 100644 app/definitions/INotification.ts create mode 100644 app/definitions/index.ts diff --git a/app/definitions/INotification.ts b/app/definitions/INotification.ts new file mode 100644 index 0000000000..77467d9d7d --- /dev/null +++ b/app/definitions/INotification.ts @@ -0,0 +1,12 @@ +export interface INotification { + message: string; + style: string; + ejson: string; + collapse_key: string; + notId: string; + msgcnt: string; + title: string; + from: string; + image: string; + soundname: string; +} diff --git a/app/definitions/index.ts b/app/definitions/index.ts new file mode 100644 index 0000000000..e60298e7cc --- /dev/null +++ b/app/definitions/index.ts @@ -0,0 +1,7 @@ +export * from './IAttachment'; +export * from './IMessage'; +export * from './INotification'; +export * from './IRocketChatRecord'; +export * from './IRoom'; +export * from './IServer'; +// export * from './ITeam'; From 18d3a60fd29efc9b977e0a33ea84f82de5d94012 Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Fri, 7 Jan 2022 11:32:16 -0300 Subject: [PATCH 5/7] chore: fix INotification import and use RoomTYpe instead string literal --- app/notifications/push/index.ts | 25 +++---------------------- app/notifications/push/push.ios.ts | 2 +- app/notifications/push/push.ts | 2 +- 3 files changed, 5 insertions(+), 24 deletions(-) diff --git a/app/notifications/push/index.ts b/app/notifications/push/index.ts index 6c4f367eb4..8f5dff1da2 100644 --- a/app/notifications/push/index.ts +++ b/app/notifications/push/index.ts @@ -4,26 +4,7 @@ import store from '../../lib/createStore'; import { deepLinkingOpen } from '../../actions/deepLinking'; import { isFDroidBuild } from '../../constants/environment'; import PushNotification from './push'; - -export interface INotification { - message: string; - style: string; - ejson: string; - collapse_key: string; - notId: string; - msgcnt: string; - title: string; - from: string; - image: string; - soundname: string; - // only for info - 'google.delivered_priority': string; - 'google.original_priority': string; - 'google.message_id': string; - 'google.sent_time': number; - 'google.c.sender.id': string; - 'google.ttl': number; -} +import { INotification, RoomType } from '../../definitions'; interface IEjson { rid: string; @@ -46,8 +27,8 @@ export const onNotification = (notification: INotification): void => { p: 'group', l: 'channels' }; - let roomName = type === 'd' ? sender.username : name; - if (type === 'l') { + let roomName = type === RoomType.DIRECT ? sender.username : name; + if (type === RoomType.OMNICHANNEL) { roomName = sender.name; } diff --git a/app/notifications/push/push.ios.ts b/app/notifications/push/push.ios.ts index 9a643cee2d..92c2d3a6ca 100644 --- a/app/notifications/push/push.ios.ts +++ b/app/notifications/push/push.ios.ts @@ -4,7 +4,7 @@ import NotificationsIOS, { NotificationAction, NotificationCategory, Notificatio import reduxStore from '../../lib/createStore'; import I18n from '../../i18n'; -import { INotification } from '.'; +import { INotification } from '../../definitions/INotification'; class PushNotification { onNotification: (notification: Notification) => void; diff --git a/app/notifications/push/push.ts b/app/notifications/push/push.ts index f708381a67..16aa8cf5ce 100644 --- a/app/notifications/push/push.ts +++ b/app/notifications/push/push.ts @@ -2,7 +2,7 @@ // TODO BUMP LIB VERSION import { NotificationsAndroid, PendingNotifications, Notification } from 'react-native-notifications'; -import { INotification } from '.'; +import { INotification } from '../../definitions/INotification'; class PushNotification { onNotification: (notification: Notification) => void; From ea86d303a87cb5f8ed6ec7635b32ddcce34f3f0f Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Wed, 12 Jan 2022 13:25:52 -0300 Subject: [PATCH 6/7] fix import --- app/definitions/index.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/definitions/index.ts b/app/definitions/index.ts index a34872120d..075f9f2127 100644 --- a/app/definitions/index.ts +++ b/app/definitions/index.ts @@ -5,10 +5,8 @@ import { Dispatch } from 'redux'; export * from './IAttachment'; export * from './IMessage'; export * from './INotification'; -export * from './IRocketChatRecord'; export * from './IRoom'; export * from './IServer'; -export * from './ITeam'; export interface IBaseScreen, S extends string> { navigation: StackNavigationProp; From c71891e7ea7636c8890689fda8ba4cc9a86f236b Mon Sep 17 00:00:00 2001 From: GleidsonDaniel Date: Wed, 12 Jan 2022 13:34:54 -0300 Subject: [PATCH 7/7] fix: change RoomType to SubscriptionType --- app/definitions/index.ts | 1 + app/notifications/push/index.ts | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/definitions/index.ts b/app/definitions/index.ts index 075f9f2127..80eeb88ca2 100644 --- a/app/definitions/index.ts +++ b/app/definitions/index.ts @@ -7,6 +7,7 @@ export * from './IMessage'; export * from './INotification'; export * from './IRoom'; export * from './IServer'; +export * from './ISubscription'; export interface IBaseScreen, S extends string> { navigation: StackNavigationProp; diff --git a/app/notifications/push/index.ts b/app/notifications/push/index.ts index 8f5dff1da2..af25c9eed9 100644 --- a/app/notifications/push/index.ts +++ b/app/notifications/push/index.ts @@ -4,7 +4,7 @@ import store from '../../lib/createStore'; import { deepLinkingOpen } from '../../actions/deepLinking'; import { isFDroidBuild } from '../../constants/environment'; import PushNotification from './push'; -import { INotification, RoomType } from '../../definitions'; +import { INotification, SubscriptionType } from '../../definitions'; interface IEjson { rid: string; @@ -27,8 +27,8 @@ export const onNotification = (notification: INotification): void => { p: 'group', l: 'channels' }; - let roomName = type === RoomType.DIRECT ? sender.username : name; - if (type === RoomType.OMNICHANNEL) { + let roomName = type === SubscriptionType.DIRECT ? sender.username : name; + if (type === SubscriptionType.OMNICHANNEL) { roomName = sender.name; }