2018-07-10 13:40:32 +00:00
|
|
|
import EJSON from 'ejson';
|
|
|
|
|
|
|
|
import PushNotification from './push';
|
|
|
|
import store from '../lib/createStore';
|
|
|
|
import { deepLinkingOpen } from '../actions/deepLinking';
|
|
|
|
|
2019-03-18 18:52:38 +00:00
|
|
|
export const onNotification = (notification) => {
|
2018-07-10 13:40:32 +00:00
|
|
|
if (notification) {
|
|
|
|
const data = notification.getData();
|
|
|
|
if (data) {
|
|
|
|
try {
|
|
|
|
const {
|
|
|
|
rid, name, sender, type, host
|
|
|
|
} = EJSON.parse(data.ejson);
|
|
|
|
|
|
|
|
const types = {
|
|
|
|
c: 'channel', d: 'direct', p: 'group'
|
|
|
|
};
|
|
|
|
const roomName = type === 'd' ? sender.username : name;
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
host,
|
|
|
|
rid,
|
|
|
|
path: `${ types[type] }/${ roomName }`
|
|
|
|
};
|
|
|
|
store.dispatch(deepLinkingOpen(params));
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-18 18:52:38 +00:00
|
|
|
export const getDeviceToken = () => PushNotification.getDeviceToken();
|
|
|
|
export const setBadgeCount = count => PushNotification.setBadgeCount(count);
|
|
|
|
export const initializePushNotifications = () => {
|
|
|
|
setBadgeCount();
|
|
|
|
return PushNotification.configure({
|
2018-07-10 13:40:32 +00:00
|
|
|
onNotification
|
|
|
|
});
|
|
|
|
};
|