Push API

Push API

  The 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.

  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().

  

  subscribe 返回一个pushSubscription.其中PushSubscription.endpoint要发送给服务器,服务器以endpoint为地址,给client发push.it is a good idea to keep your endPoint a secret, so others do not hijack it and abuse the push functionality.

navigator.serviceWorker.register(‘serviceworker.js‘).then(
  function(serviceWorkerRegistration) {
    var options = {
      userVisibleOnly: true,
      applicationServerKey: applicationServerKey
    };
    serviceWorkerRegistration.pushManager.subscribe(options).then(
      function(pushSubscription) {
        console.log(pushSubscription.endpoint);
        // The push subscription details needed by the application
        // server are now available, and can be sent to it using,
        // for example, an XMLHttpRequest.
      }, function(error) {
        // During development it often helps to log errors to the
        // console. In a production environment it might make sense to
        // also report information about errors back to the
        // application server.
        console.log(error);
      }
    );
  });

  The service worker will be started as necessary to handle incoming push messages, which are delivered to theServiceWorkerGlobalScope.onpush event handler.

  每一个endpoint都是独一无二的。不要泄露,否则其它服务器可以给你的client发push。

  当push到来时,全局的onpush属性会被调用。

this.onpush = function(event) {
  console.log(event.data);
  // From here we can write the data to IndexedDB, send it to any open
  // windows, display a notification, etc.
}

  

参考:https://developer.mozilla.org/en-US/docs/Web/API/Push_API

时间: 2024-10-20 06:34:58

Push API的相关文章

高性能的 PHP API 接口开发 视频教程

第1章 课程介绍及基础知识讲解 1-1 课程介绍 1-2 API基础知识讲解 1-3 RestfulAPI,Yaf框架,Postman讲解第2章 API接口的基本实现 2-1 准备工作-快速搭建基于Yaf框架的项目环境 2-2 用户类API-实现用户注册API.登录API 2-3 用户类API-SESSION与相关思考 2-4 文章类API-实现文章新增API.修改API 2-5 文章类API-实现文章删除API.文章状态修改API.单一文章获取详细信息API 2-6 文章类API-实现文章列表

杂项之rabbitmq

杂项之rabbitmq 本节内容 rabbitmq简介 AMQP协议 rabbitmq使用 应用举例 rabbitmq简介 介绍rabbitmq之前,先介绍一下AMQP协议,因为rabbitmq就是基于AMQP协议实现的一个服务程序.(目前为止应该也是唯一实现了AMQP协议的服务) AMQP(高级消息队列协议)是一个网络协议.它支持符合要求的客户端应用(application)和消息中间件代理(messaging middleware broker)之间进行通信. arbbitmq使用erlan

关于openstack的Rabbitmq安装

RABBITMQ服务 一.RATTITMQ的概念 RabbitMQ 是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件). AMQP是一个定义了在应用或者组织之间传送消息的协议的开放标准 (an open standard for passing business messages between applications or organizations).AMQP 目标在于解决在两个应用之间传送消息存在的下列问题: · 网络是不可靠的 =>消息需要保存后再转发并有出

Android 高仿微信实时聊天 基于百度云推送

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38799363 ,本文出自:[张鸿洋的博客] 一直在仿微信界面,今天终于有幸利用百度云推送仿一仿微信聊天了~~~ 首先特别感谢:weidi1989分享的Android之基于百度云推送IM ,大家可以直接下载:省了很多事哈,本例中也使用了weidi的部分代码,凡是@author way的就是weidi1989的代码~~ 1.效果图 核心功能也就上面的两张图了~~~我拿着手机和模拟器

云推送 和 微信多客服平台

百度云推送流程: 百度云推送服务支持三种推送类型:通知.消息及富媒体:支持向所有用户.根据标签分类向特定用户群体.向单个用户和基于地理位置推送消息:支持更多自定义功能(如自定义内容.后续行为.样式模板等):提供用户信息及通知消息统计信息,方便开发者进行后续开发及运营. 消息:目前仅支持Android平台.应用内推消息,不会有界面的提示. 通知:会有标题.内容,界面提示. 名称解释: API Key : 应用标识,终端上的绑定和服务端推送消息时都要用到. Secret Key : 应用私钥,服务端

webapi filter 全局 及 只返回json格式

在webapi中定义全局的过滤器要在app_start下WebApiConfig.cs中 public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "push.api.v1", routeTemplate: "v1/{co

[Falcor] Indroduce to Model

How to work with JSON data indirectly through a Falcor Model. The Falcor Model allows you to work with data using the same familiar JavaScript path syntax. However the Model uses a push API, sending the data to a callback rather than returning it imm

RabbitMQ学习之:(七)Fanout Exchange (转贴+我的评论)

From:http://lostechies.com/derekgreer/2012/05/16/rabbitmq-for-windows-fanout-exchanges/ PunCha: There is not too much to say about this topic.... RabbitMQ for Windows: Fanout Exchanges Posted by Derek Greer on May 16, 2012 This is the sixth installme

RabbitMQ学习之:(五)Exchange Type (转贴+我的评论)

From: http://lostechies.com/derekgreer/2012/03/28/rabbitmq-for-windows-exchange-types/ RabbitMQ for Windows: Exchange Types Posted by Derek Greer on March 28, 2012 This is the fourth installment to the series: RabbitMQ for Windows.  In thelast instal