javascript - 如何在 HTML5 Web 应用程序中进行推送通知?

标签 javascript html push-notification web-push push-api

我有一个网络应用程序。当用户在特定页面上时,我想使用 HTML 5 内置通知 API 从服务器推送通知。可能吗?

最佳答案

您现在可以使用 Service Workers 在 Chrome 中使用 Web 应用程序进行真正的推送通知和 PushManager来自 W3C Push API .

参见文章 Push Notifications on the Open Web了解您可以使用的分步操作方法和代码片段。这是那篇文章中的一张图表,解释了它周围的 UI 是什么样子。

enter image description here

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/

相关文章:

javascript - 如何通过数据属性过滤选择器结果?

javascript - 我如何在 '\' 的字符串中替换所有 '/' 和 '-' 字符,在 javascript 中

html - 新行中第一个和最后一个内联 block 元素的选择器

html - 全屏无滚动条图像轮播

android - 推送通知的最低 Google Play 服务版本

javascript - Bootstrap 。滚动链接菜单并修复页面顶部的导航栏

javascript - 使用javascript的mongodb shell中的变量集合名称

javascript - Bootstrap datetimepicker - 不会显示日历或格式化日期

android - 在 Android 中获取通知接收时间

php - 无法使用php向ios发送推送通知