From 59c02ce5d1cb3fdc1f5d9f1d061d5d95a663ffc5 Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Fri, 18 Jun 2021 19:15:40 +0300 Subject: [PATCH] Fix focusing tab on notification's click --- .../appManagers/appNotificationsManager.ts | 3 +- src/lib/appManagers/appRuntimeManager.ts | 53 +++++++++++++++++++ src/lib/mtproto/mtprotoworker.ts | 3 +- src/lib/mtproto/webPushApiManager.ts | 4 +- 4 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 src/lib/appManagers/appRuntimeManager.ts diff --git a/src/lib/appManagers/appNotificationsManager.ts b/src/lib/appManagers/appNotificationsManager.ts index d60f4bdf..8a333014 100644 --- a/src/lib/appManagers/appNotificationsManager.ts +++ b/src/lib/appManagers/appNotificationsManager.ts @@ -25,6 +25,7 @@ import stateStorage from "../stateStorage"; import apiUpdatesManager from "./apiUpdatesManager"; import appChatsManager from "./appChatsManager"; import appPeersManager from "./appPeersManager"; +import appRuntimeManager from "./appRuntimeManager"; import appStateManager from "./appStateManager"; import appUsersManager from "./appUsersManager"; @@ -599,7 +600,7 @@ export class AppNotificationsManager { notification.onclick = () => { notification.close(); - //AppRuntimeManager.focus(); + appRuntimeManager.focus(); this.clear(); if(data.onclick) { data.onclick(); diff --git a/src/lib/appManagers/appRuntimeManager.ts b/src/lib/appManagers/appRuntimeManager.ts new file mode 100644 index 00000000..fc309459 --- /dev/null +++ b/src/lib/appManagers/appRuntimeManager.ts @@ -0,0 +1,53 @@ +/* + * https://github.com/morethanwords/tweb + * Copyright (C) 2019-2021 Eduard Kuzmenko + * https://github.com/morethanwords/tweb/blob/master/LICENSE + * + * Originally from: + * https://github.com/zhukov/webogram + * Copyright (C) 2014 Igor Zhukov + * https://github.com/zhukov/webogram/blob/master/LICENSE + */ + +export class AppRuntimeManager { + public reload() { + try { + location.reload(); + } catch(e) {}; + + // if(window.chrome && chrome.runtime && chrome.runtime.reload) { + // chrome.runtime.reload(); + // } + } + + public close() { + try { + window.close(); + } catch(e) {} + } + + /** + * Better to call from event + */ + public focus() { + // // @ts-ignore + // if(window.navigator.mozApps && document.hidden) { + // // Get app instance and launch it to bring app to foreground + // // @ts-ignore + // window.navigator.mozApps.getSelf().onsuccess = function() { + // this.result.launch(); + // }; + // } else { + // // @ts-ignore + // if(window.chrome && chrome.app && chrome.app.window) { + // // @ts-ignore + // chrome.app.window.current().focus(); + // } + + window.focus(); + // } + } +} + +const appRuntimeManager = new AppRuntimeManager(); +export default appRuntimeManager; diff --git a/src/lib/mtproto/mtprotoworker.ts b/src/lib/mtproto/mtprotoworker.ts index d0d60b96..1e39db92 100644 --- a/src/lib/mtproto/mtprotoworker.ts +++ b/src/lib/mtproto/mtprotoworker.ts @@ -25,6 +25,7 @@ import singleInstance from './singleInstance'; import sessionStorage from '../sessionStorage'; import webPushApiManager from './webPushApiManager'; import AppStorage from '../storage'; +import appRuntimeManager from '../appManagers/appRuntimeManager'; type Task = { taskId: number, @@ -102,7 +103,7 @@ export class ApiManagerProxy extends CryptoWorkerMethods { sessionStorage.clear(), webPushApiManager.forceUnsubscribe() ]).finally(() => { - location.reload(); + appRuntimeManager.reload(); }); }); diff --git a/src/lib/mtproto/webPushApiManager.ts b/src/lib/mtproto/webPushApiManager.ts index e5e3015a..c1feeb62 100644 --- a/src/lib/mtproto/webPushApiManager.ts +++ b/src/lib/mtproto/webPushApiManager.ts @@ -18,6 +18,7 @@ import { ServiceWorkerNotificationsClearTask, ServiceWorkerPingTask, ServiceWork import apiManager from "./mtprotoworker"; import I18n, { LangPackKey } from "../langPack"; import { isMobile } from "../../helpers/userAgent"; +import appRuntimeManager from "../appManagers/appRuntimeManager"; export type PushSubscriptionNotifyType = 'init' | 'subscribe' | 'unsubscribe'; export type PushSubscriptionNotifyEvent = `push_${PushSubscriptionNotifyType}`; @@ -211,8 +212,7 @@ export class WebPushApiManager { apiManager.addServiceWorkerTaskListener('push_click', (task: ServiceWorkerPushClickTask) => { if(rootScope.idle && rootScope.idle.deactivated) { - // AppRuntimeManager.reload(); // WARNING - location.reload(); + appRuntimeManager.reload(); return; }