mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
Notifications Configurations added for web
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
/* eslint-disable no-unused-vars */
|
||||
/* eslint-disable no-undef */
|
||||
/* eslint-disable no-restricted-globals */
|
||||
|
||||
const regex = /{{\s*([^}]+)\s*}}/g;
|
||||
var _pushVaribales = "";
|
||||
|
||||
function interpolate(messageData) {
|
||||
return function interpolate(o) {
|
||||
return messageData.replace(regex, function (a, b) {
|
||||
var r = o[b];
|
||||
return typeof r === 'string' || typeof r === 'number' ? r : a;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function createTemplateMessage(messageData) {
|
||||
if (Object.keys(_pushVaribales).length > 0 ) {
|
||||
var message = interpolate(messageData)(_pushVaribales);
|
||||
return message;
|
||||
} else {
|
||||
return messageData;
|
||||
}
|
||||
}
|
||||
|
||||
function displayNotification(event) {
|
||||
var messageJson = event.data.text();
|
||||
messageJson = JSON.parse(messageJson);
|
||||
var title = messageJson.title ? messageJson.title : "New message";
|
||||
var imageUrl = messageJson.iconUrl ? messageJson.iconUrl : "images/icon.png";
|
||||
// var tagJson = messageJson.payload;
|
||||
// var tag = tagJson.tag ? tagJson.tag : "";
|
||||
var bodyAlert = messageJson.alert ? messageJson.alert : "Example message"
|
||||
var payloadData = messageJson.payload ? messageJson.payload : "Example message"
|
||||
let messageTemp;
|
||||
if ((messageTemp = regex.exec(bodyAlert)) !== null) {
|
||||
bodyAlert = createTemplateMessage(bodyAlert);
|
||||
}
|
||||
self.registration.showNotification(title, {
|
||||
body: bodyAlert,
|
||||
icon: imageUrl,
|
||||
data: payloadData,
|
||||
// tag: tag
|
||||
});
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
|
||||
function triggerSeenEvent(strMsg) {
|
||||
send_message_to_all_clients("msgEventSeen:" + strMsg);
|
||||
}
|
||||
|
||||
function triggerOpenEvent(strMsg) {
|
||||
send_message_to_all_clients("msgEventOpen:" + strMsg);
|
||||
}
|
||||
|
||||
function onPushNotificationReceived(event) {
|
||||
console.log('Push notification received : ', event);
|
||||
if (event.data) {
|
||||
console.log('Event data is : ', event.data.text());
|
||||
}
|
||||
event.waitUntil(displayNotification(event).then(() => triggerSeenEvent(event.data.text())));
|
||||
};
|
||||
|
||||
self.addEventListener('push', onPushNotificationReceived);
|
||||
|
||||
function send_message_to_client(client, msg) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
var msg_chan = new MessageChannel();
|
||||
|
||||
msg_chan.port1.onmessage = function (event) {
|
||||
if (event.data.error) {
|
||||
reject(event.data.error);
|
||||
} else {
|
||||
resolve(event.data);
|
||||
}
|
||||
};
|
||||
|
||||
client.postMessage(msg, [msg_chan.port2]);
|
||||
});
|
||||
}
|
||||
|
||||
function send_message_to_all_clients(msg) {
|
||||
clients.matchAll().then(clients => {
|
||||
clients.forEach(client => {
|
||||
send_message_to_client(client, msg);
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
self.addEventListener('install', function (event) {
|
||||
self.skipWaiting();
|
||||
console.log('Installed Service Worker : ', event);
|
||||
//event.postMessage("SW Says 'Hello back!'");
|
||||
});
|
||||
|
||||
self.addEventListener('message', function (event) {
|
||||
replyPort = event.ports[0];
|
||||
_pushVaribales = event.data;
|
||||
});
|
||||
|
||||
self.addEventListener('activate', function (event) {
|
||||
console.log('Activated Service Worker : ', event);
|
||||
event.waitUntil(self.clients.claim());
|
||||
});
|
||||
|
||||
self.addEventListener('notificationclick', function (event) {
|
||||
console.log('Notification clicked with tag' + event.notification.tag + " and data " + event.notification.data);
|
||||
let nidjson = event.notification.data;
|
||||
event.notification.close();
|
||||
event.waitUntil(triggerOpenEvent(nidjson));
|
||||
});
|
||||
|
||||
self.addEventListener('pushsubscriptionchange', function () {
|
||||
console.log('Push Subscription change');
|
||||
send_message_to_all_clients("updateRegistration:");
|
||||
});
|
||||
+29
-32
@@ -1,37 +1,34 @@
|
||||
function wlCommonInit() {
|
||||
|
||||
WLAuthorizationManager.obtainAccessToken("").then(
|
||||
(token) => {
|
||||
console.log('MobileFirst Server connect: Success ' + token);
|
||||
|
||||
var resourceRequest = new WLResourceRequest("/adapters/javaAdapter/resource/greet/",
|
||||
WLResourceRequest.GET
|
||||
);
|
||||
|
||||
resourceRequest.setQueryParameter("name", "world");
|
||||
resourceRequest.send().then(
|
||||
(response) => {
|
||||
// Will display "Hello world" in an alert dialog.
|
||||
console.log("Connect with JavaAdapter Success: " + response.responseText);
|
||||
//this.MFPushNotification();
|
||||
},
|
||||
(error) => {
|
||||
alert("Connect with JavaAdapter Failure: " + JSON.stringify(error));
|
||||
}
|
||||
);
|
||||
}, (error) => {
|
||||
console.log('MobileFirst Server connect: failure ' + error.responseText);
|
||||
console.log(JSON.stringify(error))
|
||||
/* this.zone.run(() => {
|
||||
alert("Bummer...");
|
||||
alert("Failed to connect to MobileFirst Server");
|
||||
}); */
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
function mobileFirstTest() {
|
||||
WLAuthorizationManager.obtainAccessToken("").then(
|
||||
(token) => {
|
||||
console.log('MobileFirst Server connect: Success ' + token);
|
||||
|
||||
var resourceRequest = new WLResourceRequest("/adapters/javaAdapter/resource/greet/",
|
||||
WLResourceRequest.GET
|
||||
);
|
||||
|
||||
resourceRequest.setQueryParameter("name", "world");
|
||||
resourceRequest.send().then(
|
||||
(response) => {
|
||||
// Will display "Hello world" in an alert dialog.
|
||||
console.log("Connect with JavaAdapter Success: " + response.responseText);
|
||||
//this.MFPushNotification();
|
||||
},
|
||||
(error) => {
|
||||
alert("Connect with JavaAdapter Failure: " + JSON.stringify(error));
|
||||
}
|
||||
);
|
||||
}, (error) => {
|
||||
console.log('MobileFirst Server connect: failure ' + error.responseText);
|
||||
console.log(JSON.stringify(error))
|
||||
/* this.zone.run(() => {
|
||||
alert("Bummer...");
|
||||
alert("Failed to connect to MobileFirst Server");
|
||||
}); */
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ function wlCommonInit(){
|
||||
// Override the default Direct Update interface.
|
||||
|
||||
//uncomment below function to get custom DU
|
||||
/* wl_directUpdateChallengeHandler.handleDirectUpdate = function(directUpdateData, directUpdateContext) {
|
||||
wl_directUpdateChallengeHandler.handleDirectUpdate = function(directUpdateData, directUpdateContext) {
|
||||
// Create a dialog.
|
||||
navigator.notification.confirm(
|
||||
'Actualização Disponivel',
|
||||
@@ -17,6 +17,6 @@ function wlCommonInit(){
|
||||
'Actualização Disponivel',
|
||||
['Actualizar']
|
||||
);
|
||||
}; */
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"name": "com.gpr.gabinetedigital",
|
||||
"gcm_sender_id": "800733765231"
|
||||
}
|
||||
Reference in New Issue
Block a user