2017-11-18 20:17:24 +00:00
|
|
|
import PushNotification from 'react-native-push-notification';
|
|
|
|
import { AsyncStorage } from 'react-native';
|
2017-11-28 17:47:56 +00:00
|
|
|
import EJSON from 'ejson';
|
|
|
|
import { goRoom } from './containers/routes/NavigationService';
|
2017-11-18 20:17:24 +00:00
|
|
|
|
2017-11-28 17:47:56 +00:00
|
|
|
const handleNotification = (notification) => {
|
2018-02-06 17:57:03 +00:00
|
|
|
if (!notification.userInteraction) {
|
2017-11-28 17:47:56 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-02-06 17:57:03 +00:00
|
|
|
const {
|
|
|
|
rid, name, sender, type
|
|
|
|
} = EJSON.parse(notification.ejson || notification.data.ejson);
|
|
|
|
return rid && goRoom({ rid, name: type === 'd' ? sender.username : name });
|
2017-11-28 17:47:56 +00:00
|
|
|
};
|
2017-11-18 20:17:24 +00:00
|
|
|
PushNotification.configure({
|
|
|
|
|
|
|
|
// (optional) Called when Token is generated (iOS and Android)
|
|
|
|
async onRegister({ token }) {
|
|
|
|
AsyncStorage.setItem('pushId', token);
|
|
|
|
},
|
|
|
|
|
|
|
|
// (required) Called when a remote or local notification is opened or received
|
2017-11-28 17:47:56 +00:00
|
|
|
onNotification: handleNotification,
|
2017-11-18 20:17:24 +00:00
|
|
|
|
|
|
|
// ANDROID ONLY: GCM Sender ID (optional - not required for local notifications, but is need to receive remote push notifications)
|
|
|
|
senderID: '673693445664',
|
|
|
|
|
|
|
|
// IOS ONLY (optional): default: all - Permissions to register.
|
|
|
|
permissions: {
|
|
|
|
alert: true,
|
|
|
|
badge: true,
|
|
|
|
sound: true
|
|
|
|
},
|
|
|
|
|
|
|
|
// Should the initial notification be popped automatically
|
|
|
|
// default: true
|
2017-11-28 17:47:56 +00:00
|
|
|
popInitialNotification: true,
|
2017-11-18 20:17:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* (optional) default: true
|
|
|
|
* - Specified if permissions (ios) and token (android and ios) will requested or not,
|
|
|
|
* - if not, you must call PushNotificationsHandler.requestPermissions() later
|
|
|
|
*/
|
|
|
|
requestPermissions: true
|
|
|
|
});
|