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

Push API的更多相关文章

  1. C#—ASP.NET:集成极光推送(Push API v3)

    C#—ASP.NET:集成极光推送(Push API v3) 原文地址: https://blog.csdn.net/CXLLLK/article/details/86489994   1.极光推送官 ...

  2. PHP 下基于 php-amqp 扩展的 RabbitMQ 简单用例 (四) -- Push API 和 Pull API

    RabbitMQ 中针对消息的分发提供了 Push API (订阅模式) 和 Pull API (主动获取) 两种模式. 在 PHP 中, 这两种模式分别通过 AMQPQueue 类中的 consum ...

  3. 信鸽推送Push API

    目录 信鸽推送 push API 0. 基本 push 1. 根据 token list,推送到android和ios 2. 推送到android和ios 所有用户 信鸽推送 push API 参考: ...

  4. JPush API client library for C Sharp(极光推送API)

    概述 这是 JPush REST API 的 C# 版本封装开发包,是由极光推送官方提供的,一般支持最新的 API 功能. 对应的 REST API 文档:http://docs.jpush.io/s ...

  5. puppeteer(五)chrome启动参数列表API

    List of Chromium Command Line Switches https://peter.sh/experiments/chromium-command-line-switches/ ...

  6. 推送消息 web push notification

    参考 : https://developers.google.com/web/fundamentals/engage-and-retain/push-notifications/   ( step b ...

  7. Gitlab Webhooks, External Services, and API(二)

    一. 使用webhooks webhook 是一个API的概念,并且变得越来越流行.我们能用事件描述的事物越多,webhook的作用范围也就越大.webhook作为 个轻量的事件处理应用,正变得越来越 ...

  8. RabbitMQ.Client API (.NET)中文文档

    主要的名称空间,接口和类 核心API中定义接口和类 RabbitMQ.Client 名称空间: 1 using RabbitMQ.Client; 核心API接口和类 IModel :表示一个AMQP ...

  9. Poloniex API 文档

    Examples PHP wrapper by compcentral: http://pastebin.com/iuezwGRZ Python wrapper by oipminer: http:/ ...

随机推荐

  1. [Lua]table(二):删除与排序

    function PrintTable(tb) for k,v in pairs(tb) do print(v) end print("-------------------") ...

  2. python学习笔记_week22

    note 知识点概要 - Session - CSRF - Model操作 - Form验证(ModelForm) - 中间件 - 缓存 - 信号 内容详细: 1. Session 基于Cookie做 ...

  3. iOS基础知识之排序

    1.字符串比较 - (NSComparisonResult)compare:(NSString *)str; 例如: NSString *str1 = @"zhangsan"; N ...

  4. python大法好——修改文件、函数

    1.修改文件 补充上次没洗完的: 想要修改某个文件里一块地方,可以使用seek函数找到位置write函数修改. 是不是觉得这很简单? 但是如果你这样操作很可能会出现乱码. 因为由于字节数的不同,编码的 ...

  5. SPring cloud (3)A Ribbon 负载均衡 配置初步

    1.引用pom <dependency> <groupId>org.springframework.cloud</groupId> <artifactId&g ...

  6. [ilink32 Error] Fatal: Unable to open file 'SDDEBUG.OBJ'

    [ilink32 Error] Fatal: Unable to open file 'SDDEBUG.OBJ' 这个路径NativeXml407\general\sdDebug.pas找不到了 修正 ...

  7. ABAP-权限查询-用户信息系统

    事务代码:SUIM

  8. ssm框架使用jsp提交表单到controller

    jsp代码: controller代码:

  9. 尚硅谷redis学习1-NOSQL简介2

    NoSql数据模型简介 聚合模型:KV键值,BSON 列族: 图形,这里的图形不是指真正的图形,而是关系图 NoSql数据库的四大分类 KV键值:BerkeleyDB,Redis,tair,memca ...

  10. TRANSFORM YOUR HABITS

    TRANSFORM YOUR HABITS3rd EditionNote from James Clear:I wrote Transform Your Habits to create a free ...