diff --git a/www/js/splash/notificationCenter.ts b/www/js/splash/notificationCenter.ts new file mode 100644 index 000000000..ca221d732 --- /dev/null +++ b/www/js/splash/notificationCenter.ts @@ -0,0 +1,20 @@ +type callbacks = { [eventName: string]: { [id: string]: Function } }; + +let notificationList: callbacks = {}; + +export const subscribe = function (eventName: string, id: string, action: Function) { + notificationList[eventName][id] = action; +} + +export const unsubscribe = function (eventName: string, id: string) { + delete notificationList[eventName][id]; +} + +export const publish = function (eventName: string, data: any = null) { + //if the event exists in the list + if (notificationList[eventName]) { + notificationList[eventName].keys().forEach((id) => { + notificationList[eventName][id](data); + }) + } +} \ No newline at end of file diff --git a/www/js/splash/pushNotifySettings.ts b/www/js/splash/pushNotifySettings.ts index 084fc0236..c70998477 100644 --- a/www/js/splash/pushNotifySettings.ts +++ b/www/js/splash/pushNotifySettings.ts @@ -13,11 +13,9 @@ * notification handling gets more complex, we should consider decoupling it as well. */ -import { getAngularService } from '../angular-react-helper'; import { updateUser } from '../commHelper'; import { logDebug, displayError } from '../plugin/logger'; - -const $rootScope = getAngularService('$rootScope'); +import { subscribe, publish } from './notificationCenter'; let push = null; const CLOUD_NOTIFICATION_EVENT = 'cloud:push:notification'; @@ -53,7 +51,7 @@ const startupInit = function () { logDebug("No additional data defined, nothing to parse"); } } - $rootScope.$emit(CLOUD_NOTIFICATION_EVENT, data); + publish(CLOUD_NOTIFICATION_EVENT, data); }); } @@ -150,9 +148,10 @@ var showDebugLocalNotification = function (message) { } const registerNotificationHandler = function () { + //in order to prevent double - registration if (!handlerRegistered) { handlerRegistered = true; - $rootScope.$on(CLOUD_NOTIFICATION_EVENT, function (event, data) { + subscribe(CLOUD_NOTIFICATION_EVENT, 'pushNotification', function (event, data) { logDebug("data = " + JSON.stringify(data)); if (data.additionalData["content-available"] == 1) { redirectSilentPush(event, data);