ozgon.md
window.addEventListener("load", function(){
loadScriptsWithCb("//www.gstatic.com/firebasejs/3.6.1/firebase.js", function(){
// Initialize Firebase
var firebaseConfig = {
apiKey: "AIzxxx",
authDomain: "xxx.firebaseapp.com",
databaseURL: "https://xxx.firebaseio.com",
storageBucket: "xxx.appspot.com",
messagingSenderId: "49952xxx300"
};
if (!firebase.apps.length) {
self.firebaseConstructor = firebase.initializeApp(firebaseConfig);
self.messaging = firebaseConstructor.messaging();
}
});
});
if (!!(<any>this.$window).Notification && (<any>this.$window).Notification.permission === "default") {
// devam
}
window.messaging.requestPermission().then(function () {
console.log('Notification izni temin edildi.');
});
window).messaging.getToken().then(function (token: any) {
sendTokenToServer(token);
});
messaging.setBackgroundMessageHandler(function(payload) {
let obj = payload.data;
const title = obj.title;
let options = {
body: obj.body,
icon: obj.icon,
timestamp: +new Date,
badge: obj.badge,
data: {
payloadr: {
memberNotificationId: obj.memberNotificationId,
click_action: obj.click_action
}
},
path: obj.click_action
}
if (!!obj.imageUrl) {
options["image"] = obj.imageUrl;
}
if (!!obj.type) {
options["tag"] = obj.type;
}
return self.registration.showNotification(title, options)
});
self.addEventListener('notificationclick', function(event) {
let obj = event.notification;
let payloaderData = obj.data.payloadr;
var checkAndMarkAsRead = function(){
if (!payloaderData.memberNotificationId) {
return;
}
var url = new URL(this.origin + "/membernotification/markasread"),
params = {Id: payloaderData.memberNotificationId};
messaging.getToken().then(function(token){
params.token = token;
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))
fetch(url).then(function(response) {
if (!response.ok) return new Error(response);
console.log("ok", response);
}).catch(function(err) {
console.log(err);
});
});
};
event.notification.close();
event.waitUntil(clients.matchAll({
type: "window"
}).then(function(clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == '/' && 'focus' in client) {
return client.focus();
}
}
if (clients.openWindow){
checkAndMarkAsRead();
return clients.openWindow(payloaderData.click_action);
}
}));
});
window.messaging.onMessage(function (payload) {
let currentUrl = window.location.href;
let payloaderData = payload.data;
let checkAndMarkAsRead = function(){
if (!payloaderData.memberNotificationId) {
return;
}
let markReadObj = {
Id: payloaderData.memberNotificationId
}
self.httpHelperService.httpPost("/membernotification/markasread", markReadObj, (res) => {
console.log("marked As Read");
window.open(payloaderData.click_action, '_blank');
}, (err)=>{
self.DialogService.warning({
error: err.Message
});
}, { isNotLoading: true });
};
self.DialogService.successWithCallback({
message: payloaderData.body,
path: payloaderData.click_action,
title: payloaderData.title,
confirm: "Hemen Görüntüle"
}, checkAndMarkAsRead);
});