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

Vic*_*ant 14 html javascript push-notification web-push push-api

我有一个Web应用程序.我想使用HTML 5内置通知API在用户位于特定页面时从服务器执行推送通知.可能吗?

sid*_*ker 15

您可以使用今天做真正的推送通知与Web应用程序在Chrome 服务工作者PushManagerW3C推式API.

有关您可以使用的分步操作方法和代码段,请参阅Open Web上的推送通知一文.这是该文章的图表,解释了它周围的UI是什么样的.

在此输入图像描述

Push API实现也已经登陆Firefox了; 它的目标是在2015年11月在Firefox 42中发布.而且微软已经表示Push API也正在考虑在Edge团队中实施.

下面是一个简单的代码示例,借鉴了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);
      }
    );
  });
Run Code Online (Sandbox Code Playgroud)

  • 客户端很容易.那么服务器端呢?这不是问题的关键吗? (4认同)