Skip to content

Commit

Permalink
Merge pull request #163 from kurnhyalcantara/notifications
Browse files Browse the repository at this point in the history
Push notification reconfigure without store token
  • Loading branch information
kurnhyalcantara committed May 29, 2021
2 parents 882ba4e + 45402f9 commit f385282
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
5 changes: 3 additions & 2 deletions data/resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"timezoneOffset": "-180",
"notifications": {
"default": "Dapatkan berita dan update kegiatan terbaru dari kami",
"enabled": "Terima kasih telah subscribe Web App kami",
"enabled": "Terima kasih telah mengaktifkan notifikasi Web App kami",
"blocked": "Kami tidak bisa mengirimkan pemberitahuan, Aktifkan pemberitahuan di browser anda",
"subscribe": "Subscribe",
"unsubscribe": "Unsubscribe",
Expand All @@ -39,7 +39,8 @@
},
"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"
},
"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(this.notifications.token));
store.dispatch(unsubscribe());
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 { getToken } from '../notifications/actions';
import { requestPermission } 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(() => {
getToken(true);
store.dispatch(requestPermission());
showToast({ message: 'Login Berhasil' });
})
.catch((error) => {
Expand Down
18 changes: 11 additions & 7 deletions src/store/notifications/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ export const initializeMessaging = () => {
},
});
});
messaging.onTokenRefresh(() => {
getToken(true);
});
resolve(messaging);
});
};

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

export const getToken = (subscribe = false) => (dispatch: Dispatch, getState) => {
export const getToken = (subscribe = false) => (dispatch: Dispatch, getState: any) => {
if (!subscribe && Notification.permission !== 'granted') {
return;
}
Expand Down Expand Up @@ -127,8 +131,8 @@ export const getToken = (subscribe = false) => (dispatch: Dispatch, getState) =>
});
};

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

0 comments on commit f385282

Please sign in to comment.