FCM Web推送通知无法单击通知链接click_action

问题描述 投票:0回答:1

我在click_action函数中添加了push参数。这是我的示例代码。

self.addEventListener("push", function(event) {
  var title = event.data.json().notification.title;
  var body = event.data.json().notification.body;
  var icon = "/assets/img/logo.png";
  var click_action =
    "http://localhost:4200/#/jobs/notification/" +
    event.data.json().data.job_no;
  event.waitUntil(
    self.registration.showNotification(title, {
      body: body,
      icon: icon,
      click_action: click_action
    })
  );
});

要处理click上的notification,定义了一个notificationclick事件处理程序。

例如:

self.addEventListener('notificationclick', function(event) {
  var redirect_url = event.notification.data.click_action;
  event.notification.close();
  event.waitUntil(
    clients
      .matchAll({
        type: "window"
      })
      .then(function(clientList) {
        console.log(clientList);
        for (var i = 0; i < clientList.length; i++) {
          var client = clientList[i];
          if (client.url === "/" && "focus" in client) {
            return client.focus();
          }
        }
        if (clients.openWindow) {
          return clients.openWindow(redirect_url);
        }
      })
  );
});

但我无法在click_action函数中获得notificationclick参数。我想将推送通知重定向到click_action中的给定网址。

请帮我。

angular firebase firebase-cloud-messaging service-worker
1个回答
1
投票

我已经解决了这个问题。我已经在请求中发送了额外的参数。这是我的示例代码:

self.addEventListener("push", function(event) {
  var title = event.data.json().notification.title;
  var body = event.data.json().notification.body;
  var icon = "/assets/img/logo.png";
  var click_action =
    "http://localhost:4200/#/jobs/notification/" +
    event.data.json().data.job_no;
  event.waitUntil(
    self.registration.showNotification(title, {
      body: body,
      icon: icon,
      data: {
        click_action
      }
    })
  );
});

并使用click_action,这里是示例代码:

self.addEventListener('notificationclick', function(event) {
  var redirect_url = event.notification.data.click_action;
  event.notification.close();
  event.waitUntil(
    clients
      .matchAll({
        type: "window"
      })
      .then(function(clientList) {
        console.log(clientList);
        for (var i = 0; i < clientList.length; i++) {
          var client = clientList[i];
          if (client.url === "/" && "focus" in client) {
            return client.focus();
          }
        }
        if (clients.openWindow) {
          return clients.openWindow(redirect_url);
        }
      })
  );
});
© www.soinside.com 2019 - 2024. All rights reserved.