node.js - 当用户在 Node.JS 中单击 pwa、angular 6 和 web-push 中的通知时路由到 url

标签 node.js angular6 web-push pwa

我正在使用 Angular6 中的 SwPush 在 PWA 中推送通知。为此,我使用@angular/service-worker。我能够从 Node 服务器发送通知。但是当我点击收到的通知时,它无法路由所需的 url。我正在使用 web-push module in node.js ,这是有效载荷:

        "payload": {
        "title": "Sample Notification",
        "actions": [
          {
            "action": "opentweet",
            "url":"https://pwa-sw-push-notifications.firebaseapp.com",
            "title": "Open tweet"
          }
        ],
        "body": "A sample data to check the notification availability!",
        "dir": "auto",
        "icon": "https://pwa-sw-push-notifications.firebaseapp.com/assets/icons/icon-72x72.png",
        "badge": "https://pwa-sw-push-notifications.firebaseapp.com/assets/icons/icon-72x72.png",
        "lang": "en",
        "url": "https://pwa-sw-push-notifications.firebaseapp.com",
        "renotify": true,
        "requireInteraction": true,
        "tag": 926796012340920300,
        "vibrate": [
          100,
          50,
          100
        ],
        "data": {
          "url": "https://pwa-sw-push-notifications.firebaseapp.com",
          "favorite_count": 0,
          "retweet_count": 0
        }
      }

我在用
var webPush=require('web-push');

//sub is userSubscriptionObject to send the notifications to subscribed user browser.
webPush.sendNotification(sub, JSON.stringify(payload))))

在角度,

export class IndexComponent implements OnInit {
  users: any = [];
  constructor(
    private http: HttpClient, 
    private router: Router, 
    private swPush: SwPush) {
    console.log('this is index');
  }
  ngOnInit() {
    this.getCoins();
  }
  subscribeNotifications(){
        this.swPush.requestSubscription({
      serverPublicKey: "BMW3STH0ODuNdhFVAZIy8FUDEwt2f8LLpWBiBnz8WE0_558rZc4aLbZUD9y-HxMlfCtyE5OD0mk3xa2oFJZu5n0"
    }).then(sub => {
        console.log("Notification Subscription: ", sub);
        this
        .http
        .post('https://e1a2e469.ngrok.io/sub/subscribeNotifications', sub);.subscribe(
          () => {
            console.log('Sent push subscription object to server.');        
        },
          err => console.log('Could not send subscription object to server, reason: ', err)
        );
      })
      .catch(err => console.error("Could not subscribe to notifications", err));
  }
  unsubscribeNotifications(){
    this.swPush.subscription.pipe(take(1)).subscribe(subscriptionValue=>{
      console.log('this is un subscription', subscriptionValue);
        this
        .http
        .post('https://e1a2e469.ngrok.io/sub/unsubscribeNotifications', subscriptionValue);
          .subscribe(
          res => {
            console.log('[App] Delete subscriber request answer', res)
            // Unsubscribe current client (browser)
            subscriptionValue.unsubscribe()
              .then(success => {
                console.log('[App] Unsubscription successful', success)
              })
              .catch(err => {
                console.log('[App] Unsubscription failed', err)
              })
          },
          err => {
            console.log('[App] Delete subscription request failed', err)
          }
    );
  });
}
  sendNotifications(){
    console.log('subscribeNotifications')
    this
      .http
      .post('https://e1a2e469.ngrok.io/sub/sendNotifications', null).subscribe(res=>{
      console.log('subscribed successfully');
    });
  }
}


我的意图是,当用户点击它应该路由到的通知时
https://pwa-sw-push-notifications.firebaseapp.com .

我尝试在上述对象中以不同的参数给出这个 URL,比如 actions[0].action.url URL data.url没有任何效果。所以我很困惑在哪里提供 URL 以使其成为可能。谁能帮帮我?

谢谢你。

最佳答案

据我所知,您的有效载荷没有任何问题。有效负载只是传递给浏览器的数据,在浏览器端你需要实现 notificationClick处理程序以处理有效负载数据,即导航到所需的 URL。一个简单的notifictionClick处理程序可以在服务 worker 中实现如下:

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));
    }
  });

如需引用,请查看以下链接:
https://github.com/angular/angular/issues/20956#issuecomment-374133852
https://medium.com/google-developer-experts/a-new-angular-service-worker-creating-automatic-progressive-web-apps-part-2-practice-3221471269a1

关于node.js - 当用户在 Node.JS 中单击 pwa、angular 6 和 web-push 中的通知时路由到 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50967365/

相关文章:

javascript - navigator.serviceWorker 用于开发环境

node.js - Coffeescript --watch开关不适用于 Node 0.4.7

angular - 在 Angular 主导出中航行时,保留次要路线

angular - 我希望我的 Angular 6 应用可以与 Google Jobs 一起使用,但无法正确格式化

progressive-web-apps - 取消发送的 Web 推送通知

push-notification - 真的有必要在每次页面加载时更新订阅吗?

node.js - Can't query date from MongoDB(Mlab) -- 如此简单但无法解决

javascript - 在Node.js和Express应用程序中重写URL

javascript - 如何获取变量的值?

html - Angular 生成的 css 破坏了子组合器