Web push notifications in chrome

2019-08-17 11:47发布

问题:

I have created web push notifications and they work in Firefox ok (service worker is registered, subscription created, subscription data stored, using subscription data notification is sent ok). I have tried the same in Chrome and Opera, but nothing happens. I tried to debug, and after I send push notification, browser receives it, executes code, but nothing happens. There are no errors, code runs till the end. Service worker code is the following:

'use strict';
self.addEventListener('push', function(event) {
  console.log('Push started');
  const promiseChain = self.registration.showNotification('Hello, World.');

  event.waitUntil(promiseChain);
  console.log('Push finished');
});

I see in console 'Push started' and 'Push finished'. Server uses https. Any ideas, what can be wrong?

回答1:

A assume you have a development domain with no https. Google Chrome only sends notifications when running with configured SSL.

How to bypass: For local development you can launch Chrome with the unsafely-treat-insecure-origin-as-secure option.

open -a /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
     --args \
     --user-data-dir=$HOME \
     --unsafely-treat-insecure-origin-as-secure=http://your-insecure-domain.dev \