2022-01-12 18:42:37 +00:00
|
|
|
import EJSON from 'ejson';
|
|
|
|
|
2022-04-12 16:37:28 +00:00
|
|
|
import { store } from '../store/auxStore';
|
2022-01-12 18:42:37 +00:00
|
|
|
import { deepLinkingOpen } from '../../actions/deepLinking';
|
2022-04-12 16:37:28 +00:00
|
|
|
import { isFDroidBuild } from '../constants';
|
2022-12-21 16:07:17 +00:00
|
|
|
import { deviceToken, pushNotificationConfigure, setNotificationsBadgeCount, removeAllNotifications } from './push';
|
2022-01-12 18:42:37 +00:00
|
|
|
import { INotification, SubscriptionType } from '../../definitions';
|
|
|
|
|
|
|
|
interface IEjson {
|
|
|
|
rid: string;
|
|
|
|
name: string;
|
|
|
|
sender: { username: string; name: string };
|
|
|
|
type: string;
|
|
|
|
host: string;
|
|
|
|
messageType: string;
|
|
|
|
messageId: string;
|
|
|
|
}
|
|
|
|
|
2022-01-19 20:37:06 +00:00
|
|
|
export const onNotification = (push: INotification): void => {
|
2022-04-07 19:14:04 +00:00
|
|
|
if (push.payload) {
|
2022-01-12 18:42:37 +00:00
|
|
|
try {
|
2022-04-07 19:14:04 +00:00
|
|
|
const notification = push.payload;
|
2022-01-12 18:42:37 +00:00
|
|
|
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 === SubscriptionType.DIRECT ? sender.username : name;
|
|
|
|
if (type === SubscriptionType.OMNICHANNEL) {
|
|
|
|
roomName = sender.name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
host,
|
|
|
|
rid,
|
|
|
|
messageId,
|
|
|
|
path: `${types[type]}/${roomName}`,
|
|
|
|
isCall: messageType === 'jitsi_call_started'
|
|
|
|
};
|
|
|
|
store.dispatch(deepLinkingOpen(params));
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-13 14:37:02 +00:00
|
|
|
export const getDeviceToken = (): string => deviceToken;
|
|
|
|
export const setBadgeCount = (count?: number): void => setNotificationsBadgeCount(count);
|
2022-12-21 16:07:17 +00:00
|
|
|
export const removeNotificationsAndBadge = () => {
|
|
|
|
removeAllNotifications();
|
|
|
|
setBadgeCount();
|
|
|
|
};
|
2022-01-12 18:42:37 +00:00
|
|
|
export const initializePushNotifications = (): Promise<INotification> | undefined => {
|
|
|
|
if (!isFDroidBuild) {
|
|
|
|
setBadgeCount();
|
2022-05-13 14:37:02 +00:00
|
|
|
return pushNotificationConfigure(onNotification);
|
2022-01-12 18:42:37 +00:00
|
|
|
}
|
|
|
|
};
|