Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

Improved handling of ServiceWorkers and their events #75

Merged
merged 1 commit into from
Oct 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Push.create('Hello World!', {
#### Available Options ####

* __body__: The body text of the notification.
* __data__: Data to pass to ServiceWorker notifications
* __icon__: Can be either the URL to an icon image or an array containing 16x16 and 32x32 pixel icon images (see above).
* __onClick__: Callback to execute when the notification is clicked.
* __onClose__: Callback to execute when the notification is closed (obsolete).
Expand Down
46 changes: 31 additions & 15 deletions push.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,17 @@
/* Set the last service worker path for testing */
self.lastWorkerPath = options.serviceWorker || 'sw.js';

/* onClose event handler */
onClose = function () {
/* A bit redundant, but covers the cases when close() isn't explicitly called */
removeNotification(id);
if (isFunction(options.onClose)) {
options.onClose.call(this);
}
};

/* Safari 6+, Firefox 22+, Chrome 22+, Opera 25+ */
if (w.Notification) {

try {
notification = new w.Notification(
title,
Expand All @@ -192,9 +200,11 @@
registration.showNotification(
title,
{
icon: options.icon,
body: options.body,
vibrate: options.vibrate,
tag: options.tag,
data: options.data,
requireInteraction: options.requireInteraction
}
);
Expand Down Expand Up @@ -262,26 +272,32 @@
}, options.timeout);
}

/* Notification callbacks */
if (isFunction(options.onShow))
notification.addEventListener('show', options.onShow);
if (typeof(notification) !== 'undefined') {
/* Notification callbacks */
if (isFunction(options.onShow))
notification.addEventListener('show', options.onShow);

if (isFunction(options.onError))
notification.addEventListener('error', options.onError);
if (isFunction(options.onError))
notification.addEventListener('error', options.onError);

if (isFunction(options.onClick))
notification.addEventListener('click', options.onClick);
if (isFunction(options.onClick))
notification.addEventListener('click', options.onClick);

onClose = function () {
/* A bit redundant, but covers the cases when close() isn't explicitly called */
removeNotification(id);
if (isFunction(options.onClose)) {
options.onClose.call(this);
notification.addEventListener('close', onClose);
notification.addEventListener('cancel', onClose);
} else if (isFunction(options.onClick)) {
/* Notification callback for service worker */
if (isFunction(options.onClick)) {
self.addEventListener('notificationclick', function(event) {
options.onClick.call(event.notification);
});
}

self.addEventListener('notificationclose', function(event) {
onClose.call(event.notification);
});
}

notification.addEventListener('close', onClose);
notification.addEventListener('cancel', onClose);

/* Return the wrapper so the user can call close() */
return wrapper;
Expand Down
2 changes: 1 addition & 1 deletion push.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.