/browser-notif

Lets a web page send notifications that are displayed outside the page at the system level

Primary LanguageTypeScriptMIT LicenseMIT

BrowserNotif.JS

contributions welcome Release Typescript Dependencies GitHub license NPM Download

Lets a web page send notifications that are displayed outside the page at the system level. This lets web apps send information to a user even if the application is idle, in the background, switched tabs or moved to a different app.

Install

npm install browser-notif --save

Demo

http://ipanardian.github.io/browser-notif

Usage

Typescript

// Import
import BrowserNotif from './BrowserNotif'

// If you want to explicitly call request permission. Usually this is only called once.
BrowserNotif.requestPermission().then(p => console.log(p))

// Create instance
let notif1 = new BrowserNotif({
    icon: 'logo.png',
    lang: 'en-US',
    timeout: 10 // How long notif will be appear in seconds
})

notif1
    .notify('First Notif', 'Hi there! Nice to meet you.', {
        click() {
            window.open('//ipanardian.com')
        },
        error() {
            //On error
        }
    })
    .then(() => {
        //Do something
    })

//close notif pragmatically
notif1.close()			

Javascript

In Javascript BrowserNotif use UMD module pattern and Polyfill for Object.assign.

BrowserNotif.default.requestPermission().then(p => console.log(p))

var notif = new BrowserNotif.default({ icon: 'icon.png' })

notif
    .notify('First Notif', 'Hi there! Nice to meet you.', {
        click: function () {
            window.open('//ipanardian.com')
        }
    })
    .then(function () {
        //Do something
    })

Notification On Mobile Devices

Notification on mobile devices is using Service Worker. A service worker is an event-driven worker registered against an origin and a path. Service worker runs in the background and only run over HTTPS.

Put file 'sw.min.js' on root directory of application

var notif = new BrowserNotif({ icon: 'icon.png', serviceWorkerPath: 'sw.min.js' })
notif
    .notify('First Notif', 'Hi there! Nice to meet you.', {
        clickOnServiceWorker(clients) {
            clients.openWindow('//ipanardian.com')
        }
    })
    .then(() => {
        //Do something
    })

gif

Build

// Install Dev Dependencies
cd browser-notif
npm install --only=dev

// Compile Typescript only
gulp 

// Compile Typescript, Babelify and Uglify
gulp build

Check 'dist' folder.

  • BrowserNotif.js
  • BrowserNotif.min.js
  • BrowserNotif.min.js.map
  • sw.min.js

Browser compatibility

If browser not support Notification API then native alert will be triggered.

Desktop

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support 5webkit[1]
22
(Yes) 4.0 moz[2]
22
No support 25 6[3]
icon 5webkit[1]
22
? 4.0 moz[2]
22
No support 25 No support
Available in workers (Yes) ? 41.0 (41.0) ? ? ?
silent 43.0 ? No support No support No support No support
noscreen, sticky No support ? No support No support No support No support
sound No support ? No support No support No support No support
renotify 50.0 ? No support No support No support No support
Promise-based Notification.requestPermission() 46.0 ? 47.0 (47.0) ? 40 No support
vibrate, actions 53.0 ?     39  
badge 53.0 ?     39  
image 55.0 ?     ?  

Mobile

Feature Android Android Webview Edge Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support ?

(Yes)

(Yes) 4.0moz[2]
22
1.0.1moz[2]
1.2
No support ? No support

(Yes)

icon ? (Yes) ? 4.0moz[2]
22
1.0.1moz[2]
1.2
No support ? No support (Yes)
Available in workers ? (Yes) ? 41.0 (41.0) ? ? ? ? (Yes)
silent No support 43.0 ? No support No support No support No support No support 43.0
noscreen, sticky No support No support ? No support No support No support No support No support No support
sound No support (Yes) ? No support No support No support No support No support (Yes)
renotify No support 50.0 ? No support No support No support No support No support No support
Promise-based Notification.requestPermission() ? ? ? 47.0 (47.0) ? ? ? ? ?
vibrate, actions No support 53.0 ?       39   53.0
badge No support 53.0 ?       39   53.0
image No support No support ?       ?   55.0

License

The MIT License (MIT)

Copyright (c) 2016 Ipan Ardian

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.