make sure the push token is sent to the server on app reload

This commit is contained in:
or-else
2019-02-08 12:43:44 +03:00
parent f710e5c76c
commit 2e110a7a78
2 changed files with 13 additions and 14 deletions
+13 -13
View File
@@ -184,17 +184,17 @@ class TinodeWeb extends React.Component {
// Initialize desktop alerts. // Initialize desktop alerts.
if (this.state.desktopAlertsEnabled) { if (this.state.desktopAlertsEnabled) {
try { try {
this.fbPush = firebase.initializeApp(FIREBASE_INIT, APP_NAME).messaging(); this.fbase = firebase.initializeApp(FIREBASE_INIT, APP_NAME).messaging();
this.fbPush.usePublicVapidKey(FIREBASE_INIT.messagingVapidKey); this.fbase.usePublicVapidKey(FIREBASE_INIT.messagingVapidKey);
navigator.serviceWorker.register('/service-worker.js').then((reg) => { navigator.serviceWorker.register('/service-worker.js').then((reg) => {
this.checkForAppUpdate(reg); this.checkForAppUpdate(reg);
this.fbPush.useServiceWorker(reg); this.fbase.useServiceWorker(reg);
this.initDesktopAlerts(); this.initDesktopAlerts();
if (this.state.desktopAlerts) { if (this.state.desktopAlerts) {
if (!this.state.firebaseToken) { if (!this.state.firebaseToken) {
this.togglePushToken(true); this.togglePushToken(true);
} else { } else {
this.tinode.setDeviceToken(this.state.firebaseToken); this.tinode.setDeviceToken(this.state.firebaseToken, true);
} }
} }
}).catch((err) => { }).catch((err) => {
@@ -817,11 +817,11 @@ class TinodeWeb extends React.Component {
initDesktopAlerts() { initDesktopAlerts() {
// Google could not be bothered to mention that // Google could not be bothered to mention that
// onTokenRefresh is never called. // onTokenRefresh is never called.
this.fbPush.onTokenRefresh(() => { this.fbase.onTokenRefresh(() => {
this.requestPushToken(true); this.requestPushToken();
}); });
this.fbPush.onMessage((payload) => { this.fbase.onMessage((payload) => {
// No need to do anything about it. // No need to do anything about it.
// All the magic happends in the service worker. // All the magic happends in the service worker.
}); });
@@ -830,8 +830,8 @@ class TinodeWeb extends React.Component {
togglePushToken(enabled) { togglePushToken(enabled) {
if (enabled) { if (enabled) {
if (!this.state.firebaseToken) { if (!this.state.firebaseToken) {
this.fbPush.requestPermission().then(() => { this.fbase.requestPermission().then(() => {
this.requestPushToken(true); this.requestPushToken();
}).catch((err) => { }).catch((err) => {
this.handleError(err.message, 'err'); this.handleError(err.message, 'err');
this.setState({desktopAlerts: false, firebaseToken: null}); this.setState({desktopAlerts: false, firebaseToken: null});
@@ -843,7 +843,7 @@ class TinodeWeb extends React.Component {
LocalStorageUtil.updateObject('settings', {desktopAlerts: true}); LocalStorageUtil.updateObject('settings', {desktopAlerts: true});
} }
} else if (this.state.firebaseToken) { } else if (this.state.firebaseToken) {
this.fbPush.deleteToken(this.state.firebaseToken).catch((err) => { this.fbase.deleteToken(this.state.firebaseToken).catch((err) => {
console.log("Unable to delete token.", err); console.log("Unable to delete token.", err);
}).finally(() => { }).finally(() => {
LocalStorageUtil.updateObject('settings', {desktopAlerts: false}); LocalStorageUtil.updateObject('settings', {desktopAlerts: false});
@@ -856,10 +856,10 @@ class TinodeWeb extends React.Component {
} }
} }
requestPushToken(sendToServer) { requestPushToken() {
this.fbPush.getToken().then((refreshedToken) => { this.fbase.getToken().then((refreshedToken) => {
if (refreshedToken != this.state.firebaseToken) { if (refreshedToken != this.state.firebaseToken) {
this.tinode.setDeviceToken(refreshedToken, sendToServer); this.tinode.setDeviceToken(refreshedToken, true);
LocalStorageUtil.setObject('firebase-token', refreshedToken); LocalStorageUtil.setObject('firebase-token', refreshedToken);
} }
this.setState({firebaseToken: refreshedToken, desktopAlerts: true}); this.setState({firebaseToken: refreshedToken, desktopAlerts: true});
-1
View File
@@ -186,7 +186,6 @@ function draftyFormatter(style, data, values, key) {
case 'FM': case 'FM':
// Form // Form
attr.className = 'bot-form'; attr.className = 'bot-form';
// console.log(data, attr);
break; break;
case 'FE': case 'FE':
// Form element formatting is dependent on element content. // Form element formatting is dependent on element content.