Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Below is a working example of desktop notifications for Chrome, Firefox, Opera and Safari. Note that for security reasons, starting with Chrome 62, <a href="https://www.chromestatus.com/feature/6451284559265792" rel="noreferrer">permission for the Notification API may no longer be requested from a cross-origin iframe</a>, so you'll need to save this example in an HTML file on your site/application, and make sure to use HTTPS.</p> <pre class="lang-js prettyprint-override"><code>// request permission on page load document.addEventListener('DOMContentLoaded', function () { if (!Notification) { alert('Desktop notifications not available in your browser. Try Chromium.'); return; } if (Notification.permission !== "granted") Notification.requestPermission(); }); function notifyMe() { if (Notification.permission !== "granted") Notification.requestPermission(); else { var notification = new Notification('Notification title', { icon: 'http://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png', body: "Hey there! You've been notified!", }); notification.onclick = function () { window.open("http://stackoverflow.com/a/13328397/1269037"); }; } } </code></pre> <pre class="lang-html prettyprint-override"><code>&lt;button onclick="notifyMe()"&gt;Notify me!&lt;/button&gt; </code></pre> <hr> <p>We're using the <a href="http://www.w3.org/TR/notifications/" rel="noreferrer">W3C Notifications</a> API, documented at <a href="https://developer.mozilla.org/en-US/docs/Web/API/notification" rel="noreferrer">MDN</a>. Do not confuse this with the <a href="https://developer.chrome.com/extensions/desktop_notifications" rel="noreferrer">Chrome extensions notifications API</a>, which is different. Chrome extension notifications obviously only work in Chrome extensions, don't require any special permission from the user, support rich text notifications, but <a href="https://code.google.com/p/chromium/issues/detail?id=493500#c3" rel="noreferrer">disappear automatically and the user may not notice they have been triggered</a>). W3C notifications work in many browsers (see support on <a href="http://caniuse.com/#search=Notification" rel="noreferrer">caniuse</a>), require user permission, stack on top of the previous notification and don't automatically disappear in Chrome (<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=875114" rel="noreferrer">they do in Firefox</a>).</p> <h3>Final words</h3> <p>Notification support has been in continuous flux, with various APIs being deprecated over the last three years. If you're curious, check the previous edits of this answer to see what used to work in Chrome, and to learn the story of rich HTML notifications.</p> <p>Now the latest standard is at <a href="https://notifications.spec.whatwg.org/" rel="noreferrer">https://notifications.spec.whatwg.org/</a>.</p> <p>There's also a <a href="https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification" rel="noreferrer">different call</a> (though with the same parameters) to create notifications from service workers, which <a href="https://bugs.chromium.org/p/chromium/issues/detail?id=590029" rel="noreferrer">for some reason</a>, don't have access to the <code>Notification()</code> constructor.</p> <p>See also <a href="https://github.com/alexgibson/notify.js/" rel="noreferrer">notify.js</a> for a helper library.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload