Add callback support to push notifications (#90)

* Add callback support to push notifications

* Syntax fix

* Fix callbacks for initial push event

* Add JWT.

* Whoops, jwt was grabbed from the wrong spot
pull/85/merge
Robbie Trencheny 2016-08-16 23:57:42 -07:00 committed by Paulus Schoutsen
parent dd6ee9544e
commit b90b8f87bc
1 changed files with 29 additions and 2 deletions

View File

@ -2,17 +2,25 @@ self.addEventListener("push", function(event) {
var data;
if (event.data) {
data = event.data.json();
event.waitUntil(self.registration.showNotification(data.title, data));
event.waitUntil(
self.registration.showNotification(data.title, data)
.then(function(notification){
firePushCallback({type: "push", tag: data.tag}, data.data.jwt);
})
);
}
});
self.addEventListener('notificationclick', function(event) {
var url;
notificationEventCallback(event);
event.notification.close();
if (!event.notification.data || !event.notification.data.url) {
return;
}
event.notification.close();
url = event.notification.data.url;
if (!url) return;
@ -37,3 +45,22 @@ self.addEventListener('notificationclick', function(event) {
})
);
});
self.addEventListener('notificationclose', function(event) {
notificationEventCallback(event);
});
function notificationEventCallback(event){
firePushCallback({
type: event.type,
tag: event.notification.tag,
action: event.action
}, event.notification.data.jwt);
}
function firePushCallback(data, jwt){
fetch('/api/notify.html5/callback', {
method: 'POST',
headers: new Headers({'Content-Type': 'application/json',
'Authorization': 'Bearer '+jwt}),
body: JSON.stringify(data)
});
}