Clicking the action part of @angular/service-worker push notifications

后端 未结 2 816
情歌与酒
情歌与酒 2021-02-10 03:58

I\'ve been trying to redirect users to the \"action\" part of Webpush coming from the backend (PHP).

    return (new WebPushMessage)
        ->title(\'Title\'         


        
相关标签:
2条回答
  • 2021-02-10 04:22

    Got a response from u-ryo on Github

    There is a workaround. Add the codes below to ngsw-worker.js around the line this.scope.addEventListener('push', (event) => this.onPush(event));(Line 1775).

      this.scope.addEventListener('notificationclick', (event) => {
        console.log('[Service Worker] Notification click Received. event', event);
        event.notification.close();
        if (clients.openWindow && event.notification.data.url) {
          event.waitUntil(clients.openWindow(event.notification.data.url));
        }
      });
    

    Then you can specify the URL in the "notification.data.url".

    https://github.com/angular/angular/issues/20956#issuecomment-374133852

    0 讨论(0)
  • 2021-02-10 04:39

    If you want to have actions, and each action with a different url, here is my fix.

        this.scope.addEventListener('notificationclick', (event) => {
            event.notification.close();
            var payload = event.notification.data;
            var url = payload.url;
            if (event.action && payload.actions && payload.actions.length) {
                var actions = payload.actions.filter(x => x.action == event.action);
                if (actions.length && actions[0].url) {
                    url = actions[0].url;
                }
            }
            if (clients.openWindow && url) {
                event.waitUtil(clients.openWindow(url));
            }
        });     
    
    0 讨论(0)
提交回复
热议问题