Commit 871e2e62 authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Use tokenSelector in pushNotifications saga and dispatch register action on push token refresh

parent 8577c76a
export const REGISTER = 'PUSH_NOTIFICATIONS_REGISTER'; export const REGISTER = 'PUSH_NOTIFICATIONS_REGISTER';
export const INVALIDATE = 'PUSH_NOTIFICATIONS_INVALIDATE'; export const INVALIDATE = 'PUSH_NOTIFICATIONS_INVALIDATE';
export function register(token) { export function register() {
return { type: REGISTER, payload: { token } }; return { type: REGISTER };
} }
export function invalidate() { export function invalidate() {
......
...@@ -45,6 +45,10 @@ FCM.on(FCMEvent.Notification, async (notif) => { ...@@ -45,6 +45,10 @@ FCM.on(FCMEvent.Notification, async (notif) => {
} }
}); });
FCM.on(FCMEvent.RefreshToken, async () => {
store.dispatch(register());
});
class Main extends Component { class Main extends Component {
componentDidMount() { componentDidMount() {
Moment.locale('nl'); Moment.locale('nl');
...@@ -59,7 +63,7 @@ class Main extends Component { ...@@ -59,7 +63,7 @@ class Main extends Component {
if (username !== null && token !== null) { if (username !== null && token !== null) {
store.dispatch(loginActions.success(username, token, displayName, photo, '')); store.dispatch(loginActions.success(username, token, displayName, photo, ''));
store.dispatch(register(token)); store.dispatch(register());
} }
}); });
} }
......
...@@ -50,7 +50,7 @@ const login = function* login(action) { ...@@ -50,7 +50,7 @@ const login = function* login(action) {
yield put(loginActions.success( yield put(loginActions.success(
user, token, displayName, avatar, user, token, displayName, avatar,
)); ));
yield put(pushNotificationsActions.register(token)); yield put(pushNotificationsActions.register());
yield delay(2000); yield delay(2000);
yield put(loginActions.reset()); yield put(loginActions.reset());
} catch (error) { } catch (error) {
......
import { call, takeEvery } from 'redux-saga/effects'; import { call, takeEvery, select } from 'redux-saga/effects';
import { Platform } from 'react-native'; import { Platform } from 'react-native';
import FCM from 'react-native-fcm'; import FCM from 'react-native-fcm';
import { apiRequest } from '../url'; import { apiRequest, tokenSelector } from '../url';
import * as pushNotificationsActions from '../actions/pushNotifications'; import * as pushNotificationsActions from '../actions/pushNotifications';
const register = function* register(action) { const register = function* register() {
const { token } = action.payload; const token = yield select(tokenSelector);
let pushToken; let pushToken;
if (Platform.OS === 'ios') { if (Platform.OS === 'ios') {
yield call(FCM.requestPermissions);
pushToken = yield call(FCM.getFCMToken); pushToken = yield call(FCM.getFCMToken);
} else { } else {
yield call(FCM.requestPermissions);
pushToken = yield call(FCM.getFCMToken); pushToken = yield call(FCM.getFCMToken);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment