Skip to content
Advertisement

How to receive and handle Chrome notifications/push-notification?

I just want to write a small script that does Foo() whenever I receive a push notification from my browser(chrome). I tried to find some stuff out there but all I found was how to send said notifications, but I only want to receive them. Anybody can lead me in the right direction?

  1. I do not own the backend that sends the notifications
  2. There is no frontend, its notification from the browser
  3. I am currently trying with a browser extension, but cant access the notification. I do not know if browser extension is the way to go, that should be clear from my initial post.

Advertisement

Answer

If the question is about intercepting notifications that are generated on the web page with the Notification API, this answer explains how: Intercept HTML5 Web Notifications in a browser environment

To sum it up, it consists in the creation of a Proxy as a wrapper of the native Notification in order to hook into its constructor and execute arbitrary code.

If the question is about intercepting Push Notifications then it is impossible because they are based on the Service worker. You can’t hook into the service worker, and you can’t register your own service worker without overriding the existing one (which will break the web page), as stated in the documentation:

If there is an existing service worker available, the new version is installed in the background, but not yet activated — at this point it is called the worker in waiting. It is only activated when there are no longer any pages loaded that are still using the old service worker. As soon as there are no more pages to be loaded, the new service worker activates (becoming the active worker).

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement