开发式平台 - Web API 接口参考 编辑
这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。
Push API 给与了Web应用程序接收从服务器发出的推送消息的能力,无论Web应用程序是否在用户设备前台,甚至刚加载完成。这样,开发人员就可以向用户投放异步通知和更新,从而让用户能更及时地获取新内容。
注意: 本文档涵盖了W3C Push API规范; 如果您正在寻找有关Firefox OS专有推送机制的文档,请参阅 Simple Push.
Push 的概念及用法
对于一个应用来说,要想要接收到推送消息,需要有一个被激活的 service worker。当 service worker 处于激活状态时,可以使用 PushManager.subscribe()
来订阅推送通知。
PushSubscription
的结果包含了应用需要发送的推送消息的所有信息:端点及发送数据需要的加密密钥。
Service worker 会在必要的时候启动并接收接下来的推送消息,传递给 ServiceWorkerGlobalScope.onpush
事件句柄。该方法允许将接收到的推送消息使用在应用上,例如通过显示一条通知(使用 ServiceWorkerRegistration.showNotification()
)
每一个订阅对 service worker 来说都是唯一的。同时订阅的端点也是一个唯一的 功能性 URL:端点的信息是给应用发送信息的全部必要条件。所以端点地址需要保证隐私,否则其他应用也可以向你的应用发送消息。
激活一个 service worker 来提供推送消息会导致资源消耗的增加,尤其是电池。不同的浏览器对此有不同的方案——目前为止还没有标准的机制。Firefox 允许对发送给应用的推送消息做数量限制(配额)。该限制会在站点每一次被访问之后刷新。相比之下,Chrome 选择不做限制,但要求站点在每一次消息到达后都显示通知,这样可以让用户确认他们仍希望接收消息并确保用户可见性。
Note: 从Gecko 44开始,当新的通知触发时,每个应用程序允许的推送消息配额不会增加,而另一个仍然可见,持续三秒钟。 这可以处理收到通知突发的情况,而不是所有通知都会产生可见通知。
Note:早于52的Chrome版本要求您在Google Cloud Messaging上设置项目以发送推送消息,并在发送推送通知时使用关联的项目编号和API密钥。 它还需要一个应用程序清单,其中包含一些使用此服务的特殊参数。
接口
PushEvent
- Represents a push action sent to the global scope of a
ServiceWorker
. It contains information sent from an application to aPushSubscription
. PushManager
- Provides a way to receive notifications from third-party servers as well as request URLs for push notifications. This interface has replaced functionality offered by the obsolete
PushRegistrationManager
interface. PushMessageData
- Provides access to push data sent by a server, and includes methods to manipulate the received data.
PushSubscription
- Provides a subcription's URL endpoint and allows unsubscription from a push service.
Service worker additions
The following additions to the Service Worker API have been specified in the Push API spec to provide an entry point for using Push messages. They also monitor and respond to push and subscription change events.
ServiceWorkerRegistration.pushManager
只读- Returns a reference to the
PushManager
interface for managing push subscriptions including subscribing, getting an active subscription, and accessing push permission status. This is the entry point into using Push messaging. ServiceWorkerGlobalScope.onpush
- An event handler fired whenever a
push
event occurs; that is, whenever a server push message is received. ServiceWorkerGlobalScope.onpushsubscriptionchange
- An event handler fired whenever a
pushsubscriptionchange
event occurs; for example, when a push subscription has been invalidated, or is about to be invalidated (e.g. when a push service sets an expiration time.)
示例
Mozilla's ServiceWorker Cookbook 包含很多关于Push有用的示例.
规范
Specification | Status | Comment |
---|---|---|
Push API | Working Draft | Initial definition |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 42.0 | 44.0 (44.0)[1][3] | 未实现[2] | ? | ? |
PushEvent.data ,PushMessageData | 未实现 | 44.0 (44.0)[3] | 未实现 | 未实现 | 未实现 |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | 未实现 | 未实现 | 48.0 (48.0)[4] | 未实现 | ?[2] | ? | ? | 42.0 |
PushEvent.data ,PushMessageData | 未实现 | 未实现 | 48.0 (48.0)[4] | 未实现 | 未实现 | 未实现 | 未实现 | 未实现 |
- [1] Currently available only on desktop versions of Firefox; also, push messages are only delivered when Firefox is running.
- [2] This is currently not implemented. See the Microsoft Edge status information.
- [3] Push (and Service Workers) have been disabled in the Firefox 45 and 52 Extended Support Releases (ESR.)
- [4] 在Firefox for Android version 48,Push已经默认开启了。
另见
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论