Skip to content

Commit

Permalink
add in notificationCenter
Browse files Browse the repository at this point in the history
trying the solution I proposed here: e-mission/e-mission-docs#1024
  • Loading branch information
Abby Wheelis committed Oct 30, 2023
1 parent 3e5df64 commit eb4ba22
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
20 changes: 20 additions & 0 deletions www/js/splash/notificationCenter.ts
Original file line number Diff line number Diff line change
@@ -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);
})
}
}
9 changes: 4 additions & 5 deletions www/js/splash/pushNotifySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit eb4ba22

Please sign in to comment.