2022-01-12 18:42:37 +00:00
|
|
|
import EJSON from 'ejson';
|
|
|
|
|
2023-11-28 20:13:45 +00:00
|
|
|
import { deepLinkingClickCallPush, deepLinkingOpen } from '../../actions/deepLinking';
|
2022-01-12 18:42:37 +00:00
|
|
|
import { INotification, SubscriptionType } from '../../definitions';
|
2023-11-24 12:46:58 +00:00
|
|
|
import { isFDroidBuild } from '../constants';
|
|
|
|
import { store } from '../store/auxStore';
|
|
|
|
import { deviceToken, pushNotificationConfigure, removeAllNotifications, setNotificationsBadgeCount } from './push';
|
2022-01-12 18:42:37 +00:00
|
|
|
|
|
|
|
interface IEjson {
|
|
|
|
rid: string;
|
|
|
|
name: string;
|
|
|
|
sender: { username: string; name: string };
|
|
|
|
type: string;
|
|
|
|
host: string;
|
|
|
|
messageId: string;
|
|
|
|
}
|
|
|
|
|
2022-01-19 20:37:06 +00:00
|
|
|
export const onNotification = (push: INotification): void => {
|
2023-11-28 20:13:45 +00:00
|
|
|
const identifier = String(push?.payload?.action?.identifier);
|
|
|
|
if (identifier === 'ACCEPT_ACTION' || identifier === 'DECLINE_ACTION') {
|
|
|
|
if (push.payload) {
|
|
|
|
const notification = EJSON.parse(push.payload.ejson);
|
|
|
|
store.dispatch(deepLinkingClickCallPush({ ...notification, event: identifier === 'ACCEPT_ACTION' ? 'accept' : 'decline' }));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
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;
|
2023-11-23 13:11:11 +00:00
|
|
|
const { rid, name, sender, type, host, messageId }: IEjson = EJSON.parse(notification.ejson);
|
2022-01-12 18:42:37 +00:00
|
|
|
|
|
|
|
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,
|
2023-11-23 13:11:11 +00:00
|
|
|
path: `${types[type]}/${roomName}`
|
2022-01-12 18:42:37 +00:00
|
|
|
};
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|