2022-01-24 20:12:36 +00:00
|
|
|
import EJSON from 'ejson';
|
|
|
|
|
2022-04-28 18:45:00 +00:00
|
|
|
import { store } from '../store/auxStore';
|
2022-01-24 20:12:36 +00:00
|
|
|
import { deepLinkingOpen } from '../../actions/deepLinking';
|
2022-04-28 18:45:00 +00:00
|
|
|
import { isFDroidBuild } from '../constants';
|
2022-05-30 22:02:08 +00:00
|
|
|
import { deviceToken, pushNotificationConfigure, setNotificationsBadgeCount } from './push';
|
2022-01-24 20:12:36 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const onNotification = (push: INotification): void => {
|
2022-04-28 18:45:00 +00:00
|
|
|
if (push.payload) {
|
2022-01-24 20:12:36 +00:00
|
|
|
try {
|
2022-04-28 18:45:00 +00:00
|
|
|
const notification = push.payload;
|
2022-01-24 20:12:36 +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-30 22:02:08 +00:00
|
|
|
export const getDeviceToken = (): string => deviceToken;
|
|
|
|
export const setBadgeCount = (count?: number): void => setNotificationsBadgeCount(count);
|
2022-01-24 20:12:36 +00:00
|
|
|
export const initializePushNotifications = (): Promise<INotification> | undefined => {
|
|
|
|
if (!isFDroidBuild) {
|
|
|
|
setBadgeCount();
|
2022-05-30 22:02:08 +00:00
|
|
|
return pushNotificationConfigure(onNotification);
|
2022-01-24 20:12:36 +00:00
|
|
|
}
|
|
|
|
};
|