2

The Push API defines a push service as

a system that allows application servers to send push messages to a webapp.

A subscription to the service is created via a call to serviceWorkerRegistration .pushManager.subscribe() without any parameters.

How is the URL for this push service configured? I would have expected .subscribe() to take a URL parameter for the service.

The Push API's sister spec, Generic Event Delivery Using HTTP Push, says:

push service: This resource is used to create push message subscriptions (see Section 3). A URL for the push service is configured into user agents.

But it does not specify how this configuration would take place.

4

2 回答 2

4

您引用的第二份文件中的这句话回答了这个问题:

本文档有意不描述如何发现推送服务。推送服务的发现留待未来的努力,如果它被证明是必要的。用户代理应该配置一个推送服务的 URL。

目前,浏览器内置了一些特定推送服务的知识,例如 Chrome 中的 GCM。这在 HTMLRocks上的 Open Web 上的推送通知文章中提到:

Chrome 使用 GCM 来处理推送消息的发送和传递……其他浏览器可以免费使用任何推送服务……

但是看看将来是否会打开浏览器会很有趣,这样,正如你所建议的,我们可以在subscribe.

于 2015-07-14T20:28:50.817 回答
2

您首先使用 注册一个 service worker navigator.serviceWorker.register('/some-service-worker.js'),然后当您调用serviceWorkerRegistration .pushManager.subscribe()this时,它seriveceWorkerRegistration指的是您刚刚注册的 service worker。

最好用一个例子来证明这一点:

navigator.serviceWorker.register('/some-service-worker.js').then(function() {
    navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
        serviceWorkerRegistration.pushManager.subscribe({userVisibleOnly: true}).then(function() {/*...*/});
    });
});
于 2015-12-02T21:03:00.953 回答