Skip to content

Angulars Service Worker - "beforePushNotification" #55641

@Flo0806

Description

@Flo0806

Which @angular/* package(s) are relevant/related to the feature request?

Angular 17.3.x and @angular/pwa

Description

Angulars ngsw-worker.js shows notification at any time a "push" event occurs. However, receiving push notifications when the app is open and in the foreground is not a good user experience. Angular's service worker does not offer a direct way to determine whether a notification should finally be displayed or not. The "onPush" method cannot be overwritten, at most it can be extended - which does not lead to the desired result.

I tried things with a custom service worker like this:

self.addEventListener("push", (event) => {
  console.log("Push event detected, but notifications are suppressed.");
  event.preventDefault();
  event.stopPropagation();

  event.waitUntil(
    (async function () {

      console.log("Received push", event.data.json());
      event.data.json().notification.title = null;

      return;
    })()
  );
});

importScripts("./ngsw-worker.js");

but without success (It doesn't matter whether importScripts is at the top or at the bottom)

Proposed solution

A option to set, or a optional function which will return a boolean, like this:

this.swPush.beforePushNotification = () => { return false; }

Alternatives considered

A way to override functions like "onPush" in the Service Worker itself.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: service-workerIssues related to the @angular/service-worker package

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions