GCM Webpush在背景中 [英] GCM webpush in background

查看:126
本文介绍了GCM Webpush在背景中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了Web推送通知.获取错误的步骤:

I implemented web push notifications. Steps to get a bug:

  1. 打开网站
  2. 订阅推送通知
  3. 通过gcm发送多次推送-一切正常
  4. 关闭网站标签
  5. 发送推送并接收两次推送"-第一个可以,第二个是该网站已在后台更新"
  6. 重新打开网站
  7. 发送推送-一切正常

我知道,当服务人员收到推送但不显示通知时,可能会发生这种情况.但是我看到正常的通知,为什么我也看到其他奇怪的通知?我可以摆脱这种行为吗?

I know this can happen when the service worker receives push and doesn't show notification. But I see normal notification, why I also see other strange notification? Can I get rid such behaviour?

推荐答案

self.addEventListener('push', function(event) {
  // this function should return promise always
}

就我而言:

self.addEventListener('push', function(event) {
  event.waitUntil(
    self.registration.pushManager.getSubscription()
      .then(function(subscription) {
        fetch('url')
          .then(function(response) {
            self.registration.showNotification('title', {});
          });
      });
  );
}

应为:

self.addEventListener('push', function(event) {
  event.waitUntil(
    self.registration.pushManager.getSubscription()
      .then(function(subscription) {
        return fetch('url')
          .then(function(response) {
            return self.registration.showNotification('title', {});
          });
      });
  );
}

这篇关于GCM Webpush在背景中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆