这是
实验性技术
检查
浏览器兼容性表格
要小心谨慎在生产中使用这之前。
Push API gives web applications the ability to receive messages pushed to them from a server, whether or not the web app is in the foreground, or even currently loaded, on a user agent. This lets developers deliver asynchronous notifications and updates to users that opt in, resulting in better engagement with timely new content.
When implementing PushManager subscriptions, it is vitally important that you protect against CSRF/XSRF issues in your app. See the following articles for more information:
For an app to receive push messages, it has to have an active
service worker
. When the service worker is active, it can subscribe to push notifications, using
PushManager.subscribe()
.
结果
PushSubscription
includes all the information that the application needs to send a push message: an endpoint and the encryption key needed for sending data.
The service worker will be started as necessary to handle incoming push messages, which are delivered to the
ServiceWorkerGlobalScope.onpush
event handler. This allows apps to react to push messages being received, for example, by displaying a notification (using
ServiceWorkerRegistration.showNotification()
)。
Each subscription is unique to a service worker. The endpoint for the subscription is a unique capability URL : knowledge of the endpoint is all that is necessary to send a message to your application. The endpoint URL therefore needs to be kept secret, or other applications might be able to send push messages to your application.
Activating a service worker to deliver a push message can result in increased resource usage, particularly of the battery. Different browsers have different schemes for handling this, there is currently no standard mechanism. Firefox allows a limited number (quota) of push messages to be sent to an application, although Push messages that generate notifications are exempt from this limit. The limit is refreshed each time the site is visited. In comparison, Chrome applies no limit, but requires that every push message causes a notification to be displayed.
注意 : As of Gecko 44, the allowed quota of push messages per application is not incremented when a new notification fires, when another is still visible, for a period of three seconds. This handles cases where a burst of Push messages is received, and not all generate a visible notification.
注意 : Chrome versions earlier than 52 require you to set up a project on Google Cloud Messaging to send push messages, and use the associated project number and API key when sending push notifications. It also requires an app manifest, with some special parameters to use this service.
PushEvent
ServiceWorker
. It contains information sent from an application to a
PushSubscription
.
PushManager
PushRegistrationManager
接口。
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.
The following additions to the 服务工作者 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
只读
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
push
event occurs; that is, whenever a server push message is received.
ServiceWorkerGlobalScope.onpushsubscriptionchange
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 contains many useful Push examples.
| 规范 | 状态 | 注释 |
|---|---|---|
| Push API | 工作草案 | 初始定义 |
PushEvent
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
PushEvent
|
Chrome 42 |
Edge
16
Disabled
|
Firefox
44
|
IE 不支持 No | Opera 37 | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 42 |
Firefox Android
44
|
Opera Android 37 | Safari iOS 不支持 No | Samsung Internet Android 4.0 |
PushEvent()
构造函数
|
Chrome 42 |
Edge
16
Disabled
|
Firefox
44
|
IE 不支持 No | Opera 37 | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 42 |
Firefox Android
44
|
Opera Android 37 | Safari iOS 不支持 No | Samsung Internet Android 4.0 |
data
|
Chrome 57 |
Edge
16
Disabled
|
Firefox
44
|
IE 不支持 No | Opera 44 | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 57 |
Firefox Android
44
|
Opera Android 43 | Safari iOS 不支持 No | Samsung Internet Android 4.0 |
完整支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。
用户必须明确启用此特征。
PushMessageData
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
PushMessageData
|
Chrome 50 | Edge ≤79 |
Firefox
44
|
IE 不支持 No | Opera 37 | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 50 | Firefox Android 48 | Opera Android 37 | Safari iOS 不支持 No | Samsung Internet Android 5.0 |
arrayBuffer
|
Chrome 50 | Edge ≤79 |
Firefox
44
|
IE 不支持 No | Opera 37 | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 50 | Firefox Android 48 | Opera Android 37 | Safari iOS 不支持 No | Samsung Internet Android 5.0 |
blob
|
Chrome 50 | Edge ≤79 |
Firefox
44
|
IE 不支持 No | Opera 37 | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 50 | Firefox Android 48 | Opera Android 37 | Safari iOS 不支持 No | Samsung Internet Android 5.0 |
json
|
Chrome 50 | Edge ≤79 |
Firefox
44
|
IE 不支持 No | Opera 37 | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 50 | Firefox Android 48 | Opera Android 37 | Safari iOS 不支持 No | Samsung Internet Android 5.0 |
text
|
Chrome 50 | Edge ≤79 |
Firefox
44
|
IE 不支持 No | Opera 37 | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 50 | Firefox Android 48 | Opera Android 37 | Safari iOS 不支持 No | Samsung Internet Android 5.0 |
完整支持
不支持
实验。期望将来行为有所改变。
见实现注意事项。