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

Chore: Migrate notification/push to Typescript #3587

Merged
merged 9 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
50 changes: 0 additions & 50 deletions app/notifications/push/index.js

This file was deleted.

76 changes: 76 additions & 0 deletions app/notifications/push/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import EJSON from 'ejson';

import store from '../../lib/createStore';
import { deepLinkingOpen } from '../../actions/deepLinking';
import { isFDroidBuild } from '../../constants/environment';
import PushNotification from './push';

export interface INotification {
dnlsilva marked this conversation as resolved.
Show resolved Hide resolved
message: string;
style: string;
ejson: string;
dnlsilva marked this conversation as resolved.
Show resolved Hide resolved
collapse_key: string;
notId: string;
msgcnt: string;
title: string;
from: string;
image: string;
soundname: string;
// only for info
'google.delivered_priority': string;
dnlsilva marked this conversation as resolved.
Show resolved Hide resolved
'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;
}

export const onNotification = (notification: INotification): void => {
if (notification) {
try {
const { rid, name, sender, type, host, messageType, messageId }: IEjson = EJSON.parse(notification.ejson);

const types: Record<string, string> = {
c: 'channel',
d: 'direct',
p: 'group',
l: 'channels'
};
let roomName = type === 'd' ? sender.username : name;
dnlsilva marked this conversation as resolved.
Show resolved Hide resolved
if (type === 'l') {
dnlsilva marked this conversation as resolved.
Show resolved Hide resolved
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 = (): string => PushNotification.getDeviceToken();
export const setBadgeCount = (count?: number): void => PushNotification.setBadgeCount(count);
export const initializePushNotifications = (): Promise<INotification> | undefined => {
if (!isFDroidBuild) {
setBadgeCount();
return PushNotification.configure(onNotification);
}
};
32 changes: 0 additions & 32 deletions app/notifications/push/push.android.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
}

Expand All @@ -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();
AlexAlexandre marked this conversation as resolved.
Show resolved Hide resolved
return Promise.resolve(initial);
}
}
Expand Down
36 changes: 36 additions & 0 deletions app/notifications/push/push.ts
Original file line number Diff line number Diff line change
@@ -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();
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down