Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push notification reconfigure without store token #163

Merged
merged 2 commits into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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