我有一个网络应用程序。当用户在特定页面上时,我想使用 HTML 5 内置通知 API 从服务器推送通知。可能吗?
最佳答案
您现在可以使用 Service Workers 在 Chrome 中使用 Web 应用程序进行真正的推送通知和 PushManager来自 W3C Push API .
参见文章 Push Notifications on the Open Web了解您可以使用的分步操作方法和代码片段。这是那篇文章中的一张图表,解释了它周围的 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);
}
);
}
);
关于javascript - 如何在 HTML5 Web 应用程序中进行推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32501619/