基于LC push的浏览器桌面提醒快速集成方案

来源:互联网 发布:工商银行淘宝灵通卡 编辑:程序博客网 时间:2024/05/16 10:11

序言

最近要做一个桌面提醒功能,类似tower的右侧弹出式提醒,这里给出一个快速集成方案吧,因为要研究的东西蛮多的,没有太多的时间去做。

传统方案

  1. 与服务器保持长连接
    使用websocket与服务器保持长连接,监听服务器的请求,服务器端创建push给网页端,网页端收到消息。
  2. html5 Notification
    html5的notification,无论你在看哪个页面,只要有消息都应该能推送给我看到,并展示到桌面上,这就是webkitNotification要解决的问题。 Notification生成的消息不依附于某个页面,仅仅依附于浏览器。

自己搭建websocket和研究notification都有很大的学习成本,下一节介绍快速搭建方案。

基于LC push 的方案

LC ,leancloud,一个挺有名的sass服务吧,使得开发者只需要关注业务逻辑。
Github 仓库地址:https://github.com/leancloud/js-push-sdk ,

var push = AV.push({            appId: appId,            appKey: appKey        });        notify.requestPermission(0);        // 可以链式调用        push.open(function () {            console.log('可以接收推送');        });        // 监听推送消息        push.on('message', function (data) {            console.log('message');            console.log(JSON.stringify(data));            notify.createNotification("通知", {body: data.alert, icon: "/img/icon-57.jpg"});        });        // 监听网络异常        push.on('reuse', function () {            console.log('网络中断正在重试');        });        console.log(window.sessionStorage.userId)        push.subscribe([window.sessionStorage.userId], function (data) {            console.log('关注新的频道');        });

桌面提醒,推荐大写的HTML5-Desktop-Notifications
https://github.com/ttsvetko/HTML5-Desktop-Notifications

demo中用了angular,写了大段的配置,其实并没有太多的作用,平常用呢只用一行代码就够了,0,1,2的具体配置可以多研究下html5 的notification的api

notify.requestPermission(0);

在后台发推送,已解决


虽然实现了功能,但是有时间还是要对websocket和html5
的notification api进行深入研究才好O(∩_∩)O~

ITDogFire–sky

0 0