google-chromeservice-workerweb-pushpush-api

How subscribe pushManager via iframe in Chrome?


I can register service worker via iframe. When I try to run "pushManager.subscribe" I have:

DOMException: Registration failed - permission denied

This problem is only in Chrome via iframe. It works good in Firefox. And it works good without iframe in Chrome


Solution

  • Long story short, you can't! The path you are registering service worker on must be opened (be a top level domain) for it to register a service worker. One way to achieve this is:

    suppose your iframe looks like this:

     <iframe src="https://example.com"></iframe>
    

    use postMessage to communicate with iframe and ask for permission and then window.open("https://example.com/") to register the service-worker and fetch the token.

    Hope this helps :)