Home » Javascript » javascript – service worker show notification not working

javascript – service worker show notification not working

Posted by: admin February 23, 2020 Leave a comment

navigator.serviceWorker.register('/service-worker.js').then((reg) => {


Notification.requestPermission(function(result) {
    console.log('User Choice', result);
    if (result !== 'granted') {
      console.log('No notification permission granted!');
    } else {
        .then(function(swreg) {
          swreg.showNotification('Successfully subscribed!', {body:'TEST'});

This is my code in main.js

It comes to console.log('blaxblux'), but doesn’t show the notification at all. Request permission works as browser shows a popup with allow button.

What things could be the issues?

(I am using latest version of chrome)

How to&Answers:

Maybe this article can help https://developers.google.com/web/fundamentals/push-notifications/subscribing-a-user#requesting_permission

The API for getting permission recently changed from taking a callback
to returning a Promise. The problem with this, is that we can’t tell
what version of the API is implemented by the current browser, so you
have to implement both and handle both.

function askPermission() {
  return new Promise(function(resolve, reject) {
    const permissionResult = Notification.requestPermission(function(result) {

    if (permissionResult) {
      permissionResult.then(resolve, reject);
  .then(function(permissionResult) {
    if (permissionResult !== 'granted') {
      throw new Error('We weren\'t granted permission.');