javascript - 无法在 'waitUntil' 上执行 'ExtendableEvent'

标签 javascript notifications push-notification service-worker

我需要 chrome 注册 ID 将其作为参数发送给 API 调用,这样我就可以获取与注册 ID 相对应的消息。我的代码如下:

self.addEventListener('push', function(event) {

var apiPath = 'http://localhost/api/v1/notification/getNotification?regId=';
event.waitUntil(registration.pushManager.getSubscription().then(function (subscription){
    apiPath = apiPath + subscription.endpoint.split("/").slice(-1);
    event.waitUntil(fetch(apiPath).then(function(response){
        if(response.status !== 200){
            console.log("Problem Occurred:"+response.status);
            throw new Error();
        }
        return response.json().then(function(data){
            var title = data.title;
            var message = data.body;
            var icon = data.icon;
            var tag = data.tag;
            var url = data.url;
            return self.registration.showNotification(title,{
               body: message,
               icon: icon,
               tag: tag,
               data: url
            });
        })
    }).catch(function(err){
        var title = 'Notification';
        var message = 'You have new notifications';
        return self.registration.showNotification(title,{
               body: message,
               icon: '/images/Logo.png',
               tag: 'Demo',
               data: 'http://www.google.com/'
            });
    })
    )
}));
return;
});

上面的代码出现的错误是:

未捕获( promise 中)DOMException:

Failed to execute 'waitUntil' on 'ExtendableEvent': The event handler is already finished.(…) along with the extra notification 'The site has been updated in the background'. Now even if I remove event.waitUntil before the fetch(apiPath) part, I am still getting that extra notification.

请帮我找到解决方案。

P.S:问题在 Chrome Push Notification: This site has been updated in the background对我来说似乎没有任何用处。

最佳答案

您不需要多次调用event.waitUntil。您只需调用它一次并向其传递一个 Promise,事件的生命周期将延长,直到 Promise 得到解决。

self.addEventListener('push', function(event) {
  var apiPath = 'http://localhost/api/v1/notification/getNotification?regId=';

  event.waitUntil(
    registration.pushManager.getSubscription()
    .then(function(subscription) {
      apiPath = apiPath + subscription.endpoint.split("/").slice(-1);

      return fetch(apiPath)
      .then(function(response) {
        if (response.status !== 200){
          console.log("Problem Occurred:"+response.status);
          throw new Error();
        }

        return response.json();
      })
      .then(function(data) {
        return self.registration.showNotification(data.title, {
          body: data.body,
          icon: data.icon,
          tag: data.tag,
          data: data.url,
        });
      })
      .catch(function(err) {
        return self.registration.showNotification('Notification', {
          body: 'You have new notifications',
          icon: '/images/Logo.png',
          tag: 'Demo',
          data: 'http://www.google.com/'
        });
      });
    })
  );
});

关于javascript - 无法在 'waitUntil' 上执行 'ExtendableEvent',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36571510/

相关文章:

ios - 在通知中使用Pushwoosh发送数据

javascript - 在单个 Http 请求中打包多个 jQuery 模板的最佳方法?

javascript - Protractor + Selenium Chrome Docker = WebDriverError : unknown error: Chrome failed to start: exited normally

javascript - 动态更改图像颜色区域的建议

android - 在后台 30 分钟后停止 Android 杀死我的 Activity

javascript - 将新数据从 Node REST API 推送到 React-Native

javascript - 移动视口(viewport)问题间歇性

objective-c - 在 OSX 中监视屏幕保护程序事件

用于订阅/发布 MQTT(真正的小型消息代理)的 C# 客户端库

android - 通知未出现在使用 firebase 的应用程序的终止状态