如何处理非活动站点上的推送通知?

发布于 2025-01-11 13:48:49 字数 908 浏览 4 评论 0原文

我有一个 PWA 网站,当有人在该网站上拨打电话时,可以发送和接收推送通知。

我正在使用 Firebase Messaging 进行推送通知,以下是一些代码:

notification:
    {
        title:"Video call!",
        body:"Video call from:"+m,
        click_action: "https://......netlify.app/?x="+m
    },
    data:
    {
        title:"Video call!",
        email:m,
        contact_link:"https://......netlify.app/?x="+m

我在 URL 中附加了一些重要参数。结果:当网站处于焦点状态时,我会在后台收到我可以处理的 Firebase 的 onMessage 函数的通知。有用。当网站关闭时,click_action 使用参数打开网站,并且它也可以工作。唯一不起作用的是当站点处于非活动状态时(另一个选项卡在浏览器中处于焦点)。通知进来,我点击推送通知,网站变得聚焦,但没有任何变化,网站甚至没有重新加载。 URL 参数与之前相同,不会更改为链接中的参数。

那么我该怎么做才能使非活动网站更改为同一网站但使用链接中的参数呢?

我的服务人员没有做太多事情,但它就在这里,以防需要解决方案:

firebase.initializeApp({
    ....
  });

  const messaging=firebase.messaging();

  messaging.setBackgroundMessageHandler(function (payload) {
    console.log(payload);
});

I have a PWA website that can send and receive push notifications when somebody makes a call on the site.

I am using Firebase Messaging for push notifications, here is some code:

notification:
    {
        title:"Video call!",
        body:"Video call from:"+m,
        click_action: "https://......netlify.app/?x="+m
    },
    data:
    {
        title:"Video call!",
        email:m,
        contact_link:"https://......netlify.app/?x="+m

I append some important parameters in the URL. The result: When the site is in focus, I receive the notification in the background the onMessage function of Firebase which I can handle. It works. When the site is closed, the click_action opens the site with the parameters, and it's also working. The only thing that does not work is when the site is inactive (another tab is in focus in the browser). The notification comes in, I click on the push-notification, the site becomes focused, but nothing changes, the site does not even reload. The URL parameters stay the same as before, they do not change to the one in the link.

So what can I do to make the inactive website change to the same site but with the parameters in the link?

My service worker does not do much, but here it is, in case it's needed for a solution:

firebase.initializeApp({
    ....
  });

  const messaging=firebase.messaging();

  messaging.setBackgroundMessageHandler(function (payload) {
    console.log(payload);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚你发端 2025-01-18 13:48:49

通过在 Service Worker 中使用 onBackgroundMessage() 解决了这个问题,这正是针对这样的情况。

Solved it by using onBackgroundMessage() in the service worker, which is exactly for situations like this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文