【发布时间】:2015-12-06 17:24:25
【问题描述】:
我有一个网络应用程序。当用户在特定页面上时,我想使用 HTML 5 内置通知 API 从服务器推送通知。有可能吗?
【问题讨论】:
标签: javascript html push-notification web-push push-api
我有一个网络应用程序。当用户在特定页面上时,我想使用 HTML 5 内置通知 API 从服务器推送通知。有可能吗?
【问题讨论】:
标签: javascript html push-notification web-push push-api
您现在可以在 Chrome 中使用来自W3C Push API 的Service Workers 和PushManager 使用Web 应用程序进行真正的推送通知。
请参阅文章Push Notifications on the Open Web,了解您可以使用的分步操作方法和代码 sn-ps。这是那篇文章中的一张图表,它解释了它周围的 UI 是什么样的。
还有一个implementation of the Push API has already landed in Firefox;它的目标是在 2015 年 11 月在 Firefox 42 中发布。微软也有 indicated that the Push API is also under consideration for implementation in Edge team。
下面是一个简单的代码示例,借鉴自 MDN。
this.onpush = function(event) {
console.log(event.data);
}
navigator.serviceWorker.register('serviceworker.js').then(
function(serviceWorkerRegistration) {
serviceWorkerRegistration.pushManager.subscribe().then(
function(pushSubscription) {
console.log(pushSubscription.subscriptionId);
console.log(pushSubscription.endpoint);
}, function(error) {
console.log(error);
}
);
}
);
【讨论】:
这取决于你想要达到的目标:
【讨论】: