Skip to content

Commit

Permalink
Merge pull request #165 from kurnhyalcantara/notifications
Browse files Browse the repository at this point in the history
Move dispatch state notification granted before store token
  • Loading branch information
kurnhyalcantara committed May 30, 2021
2 parents f385282 + 3e41dcc commit d3f5c1a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 20 deletions.
3 changes: 2 additions & 1 deletion config/development.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"measurementId": "G-6C5J2Z2RM1"
},
"fbAppId": "811843096095915",
"googleMapApiKey": "AIzaSyAyRTnXOg03KvfYSKbcz1F_M3_gb3ESJpU"
"googleMapApiKey": "AIzaSyAyRTnXOg03KvfYSKbcz1F_M3_gb3ESJpU",
"vapidKeyFcm": "BM9glCscEYYyfvDTojjm15sx5Y-6svvW82gRej2Stj-ksOcR006gkymnvqO3jzEA9rrQuOumMW4ZmLPFEDviC78"
}
3 changes: 2 additions & 1 deletion config/production.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"measurementId": "G-6C5J2Z2RM1"
},
"fbAppId": "811843096095915",
"googleMapApiKey": "AIzaSyAyRTnXOg03KvfYSKbcz1F_M3_gb3ESJpU"
"googleMapApiKey": "AIzaSyAyRTnXOg03KvfYSKbcz1F_M3_gb3ESJpU",
"vapidKeyFcm": "BM9glCscEYYyfvDTojjm15sx5Y-6svvW82gRej2Stj-ksOcR006gkymnvqO3jzEA9rrQuOumMW4ZmLPFEDviC78"
}
2 changes: 1 addition & 1 deletion data/resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"toast": {
"title": "Buka"
},
"banner": "https://firebasestorage.googleapis.com/v0/b/lkim-asadiyah.appspot.com/o/logo%2Ficon-512.png?alt=media&token=067df503-01ac-4048-a7ad-1847749a84dc"
"icon": "https://firebasestorage.googleapis.com/v0/b/lkim-asadiyah.appspot.com/o/logo%2Ficon-144.png?alt=media&token=d7217861-d044-40ee-a771-7595ed7ac790"
},
"aboutBlock": {
"title": "Apa itu LKIM?",
Expand Down
2 changes: 1 addition & 1 deletion src/elements/header-toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export class HeaderToolbar extends ReduxMixin(PolymerElement) {
_toggleNotifications() {
this._closeNotificationMenu();
if (this.notifications.status === NOTIFICATIONS_STATUS.GRANTED) {
store.dispatch(unsubscribe());
store.dispatch(unsubscribe(this.notifications.token));
return;
}
store.dispatch(requestPermission());
Expand Down
4 changes: 2 additions & 2 deletions src/store/credential/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { store } from '..';
import { openDialog } from '../dialogs/actions';
import { DIALOGS } from '../dialogs/types';
import { WIPE_PREVIOUS_FEEDBACK } from '../feedback/types';
import { requestPermission } from '../notifications/actions';
import { getToken } from '../notifications/actions';
import { resetSubscribed } from '../subscribe/actions';
import { showToast } from '../toast/actions';
import { fetchUser } from '../users/actions';
Expand All @@ -13,7 +13,7 @@ export const signIn = (emailUser: string, passUser: string) => {
.auth()
.signInWithEmailAndPassword(emailUser, passUser)
.then(() => {
store.dispatch(requestPermission());
getToken(true);
showToast({ message: 'Login Berhasil' });
})
.catch((error) => {
Expand Down
22 changes: 8 additions & 14 deletions src/store/notifications/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,19 @@ export const initializeMessaging = () => {
},
});
});
messaging.onTokenRefresh(() => {
getToken(true);
});
resolve(messaging);
});
};

export const requestPermission = () => (dispatch: Dispatch) => {
return Notification.requestPermission()
.then(() => {
dispatch({
type: UPDATE_NOTIFICATIONS_STATUS,
status: NOTIFICATIONS_STATUS.GRANTED,
});
getToken(true);
return new Notification('Notifikasi diaktifkan', {
body: '{$ notifications.enabled $}',
image: '{$ notifications.banner $}',
});
})
.catch(() => {
Expand All @@ -46,12 +45,12 @@ export const requestPermission = () => (dispatch: Dispatch) => {
});
};

export const getToken = (subscribe = false) => (dispatch: Dispatch, getState: any) => {
export const getToken = (subscribe = false) => (dispatch: Dispatch, getState) => {
if (!subscribe && Notification.permission !== 'granted') {
return;
}
messaging
.getToken()
.getToken({ vapidKey: '{$ vapidKeyFcm $}' })
.then((currentToken) => {
if (currentToken) {
const state = getState();
Expand Down Expand Up @@ -106,11 +105,6 @@ export const getToken = (subscribe = false) => (dispatch: Dispatch, getState: an
{ merge: true }
);
}
dispatch({
type: UPDATE_NOTIFICATIONS_STATUS,
status: NOTIFICATIONS_STATUS.GRANTED,
token: currentToken,
});
}
}
);
Expand All @@ -131,8 +125,8 @@ export const getToken = (subscribe = false) => (dispatch: Dispatch, getState: an
});
};

export const unsubscribe = () => (dispatch: Dispatch) => {
return messaging.deleteToken().then(() => {
export const unsubscribe = (token) => (dispatch: Dispatch) => {
return messaging.deleteToken(token).then(() => {
dispatch({
type: UPDATE_NOTIFICATIONS_STATUS,
status: NOTIFICATIONS_STATUS.DEFAULT,
Expand Down

0 comments on commit d3f5c1a

Please sign in to comment.