Quick Start
Creating Your First Notification
Getting up and running with Push is extremely easy. Just how easy? We can create a new notification in just one line, believe it or not:
Push.create('Hello World!')
And that’s it! If the browser does not currently have user permission to show desktop notifications, it will automatically ask before proceding. Want more options? Just pass in a options object as the second parameter:
Push.create("Hello world!", {
body: "How's it hangin'?",
icon: '/icon.png',
timeout: 4000,
onClick: function () {
window.focus();
this.close();
}
});
For a full list of options, see Options & Configuration.
Closing Notifications
If you wish to close a notification before it automatically closes, you have a few options. You can either set a timeout, call Push’s close() method, or pass around the notification’s promise object and then call close() directly. Push’s close() method will only work with newer browsers, taking in a notification’s unique tag name and closing the first notification it finds with that tag:
Push.create('Hello World!', {
tag: 'foo'
});
// Somewhere later in your code...
Push.close('foo');
Alternatively, you can assign the notification promise returned by Push to a variable and close it directly using the promise’s then() method:
var promise = Push.create('Hello World!');
// Somewhere later in your code...
promise.then(function(notification) {
notification.close();
});
Please note that the functions found in the notification
wrapper will not run if the notification was generated by
a service worker. In terms of clearing all open notifications, that’s just as easy as well:
Push.clear();