Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Close the notification after destroy #10

Merged
merged 2 commits into from

2 participants

@fcsonline

It's interesting to close/hide a desktop notification after destroy it to not be overflowed by them if you create a set of them.

@fcsonline

It could be interesting to add a new property called timeout to close it automatically after specified number of seconds. What do you think about?

@alexgibson
Owner

destroy() is only called when a notification is closed or there is an error, so I'm not sure if calling close again here really makes sense.

If you want to automatically close a notification after it is shown you can use something like:

notifyShow: function () {
    setTimeout(function() {
        notification.close();
    }, 3000);
}

You can also use the tag property to control single / multiple instances of a notification.

http://www.w3.org/TR/notifications/#close-steps

@alexgibson
Owner

Also, I would be open to adding a timeout property that called close for convenience here.

PR's welcome :)

@fcsonline

Cool! But the method close is not in the notification prototype. Do you want to add it?

@alexgibson
Owner

Ah, correct - yes sure open to adding a close method (let's just leave destroy to clearing up after a notification).

@fcsonline

Ok. I'm fixing it.

@fcsonline

Check this out.

@alexgibson
Owner

Looks good to me :+1:

I'll check it out locally soon. Would be good to add a JS test for this, but I can maybe do that later if you don't have time.

Thanks for the PR!

@fcsonline

Thanks for adding tests! :)

@alexgibson alexgibson merged commit 59707ca into alexgibson:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 0 deletions.
  1. +1 −0  README.md
  2. +11 −0 notify.js
View
1  README.md
@@ -47,6 +47,7 @@ Optional parameters
* body: (string) - notification message body
* icon: (string) - path for icon to display in notification
* tag: (string) - unique identifier to stop duplicate notifications
+* timeout: (integer) - number of seconds to close the notification automatically
* notifyShow: (function) - callback when notification is shown
* notifyClose: (function) - callback when notification is closed
* notifyClick: (function) - callback when notification is clicked
View
11 notify.js
@@ -111,6 +111,7 @@
Notify.prototype.show = function () {
+ var that = this;
if (!Notify.isSupported()) {
return;
@@ -122,6 +123,12 @@
'icon' : this.options.icon
});
+ if (this.options.timeout && !isNaN(this.options.timeout)) {
+ setTimeout(function () {
+ that.myNotify.close();
+ }, this.options.timeout * 1000);
+ }
+
this.myNotify.addEventListener('show', this, false);
this.myNotify.addEventListener('error', this, false);
this.myNotify.addEventListener('close', this, false);
@@ -161,6 +168,10 @@
this.myNotify.removeEventListener('click', this, false);
};
+ Notify.prototype.close = function () {
+ this.myNotify.close();
+ };
+
Notify.prototype.handleEvent = function (e) {
switch (e.type) {
case 'show':
Something went wrong with that request. Please try again.