Extension script that runs on all tabs - Notification system

I have implemented push notifications for my extension and everything works well as long as my extension tab is open however the moment I close it I don’t get any notifications. Looked at adding a service worker but it seems it’s unavailable. Is there anything that can act like a service worker?

You mean like a background script? :slight_smile:

Well I do have background scripts and the notifications are working when the extension tab is open however when its closed the notification event does not trigger. Makes me believe that the actual code is not accessible. Either I don’t do something properly or I am completely missing something.

-EDIT-

Looking at the backend server that creates the connection channel with my extension in order to send web push notifications connects/disconnects only when the tab that has the js code is open/closed so clearly that background script does not run constantly ( not like a service worker )

Keep all the event handlers (websocket?) in the background script and if you need some data in the extension page, use the internal messaging to get it. For example runtime.sendMessage or runtime.connect.

Actually, if you are using Web Push API then you may be out of luck:

But if you are using websockets then it should work fine from the background script.

I am not using WebPush API, its a 3rd party service that establishes a connection to a server as long as the code is accessible and from my backend I send a signal that server that in turn sends a message to the extension where Notifications are being used.

Then use the library from the background script - then it will stay running while the browser is running.

As I am currently in the progress of updating my extension to use React I have used npm to install the package that I need. Can I just import the package in background scripts? I have read that there are some issues with extensions and using packages/modules, are you familiar with this?

There should be no issues. Background script is running in an invisible page using auto-generated HTML page (or using HTML file specified by developer in the manifest).
So there is not many differences compared to a normal extension page.

Previously I was using background scripts but now I switched to background page and in that page I have added the necessary scripts. Everything works as expected now.

This part really made think, if an HTML page is auto-generated then it can be accessed and this is what I needed. Thank you for your help.

1 Like