前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >CSDN 浏览器推送消息点开后是个js

CSDN 浏览器推送消息点开后是个js

作者头像
前Thoughtworks-杨焱
发布2021-12-07 20:42:19
发布2021-12-07 20:42:19
52300
代码可运行
举报
文章被收录于专栏:杨焱的专栏杨焱的专栏
运行总次数:0
代码可运行

发表于2018-12-252019-04-28 作者 wind

今天发现CSDN通过浏览器推送了好多条消息

点击进入后是这么一个内容的js,看起来就是实现推送的代码,留着,以后可能会用到的~

代码语言:javascript
代码运行次数:0
运行
复制
/*
 *
 *  Push Notifications codelab
 *  Copyright 2015 Google Inc. All rights reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 *
 */

/* eslint-env browser, serviceworker, es6 */

'use strict';
/*服务工作线程*/
// 已经发送数据给应用服务器 此时监听服务器返回数据的情况
var linkUrl = ''
var _hmt = self._hmt || [];
self.addEventListener('push', function (event) {
    // console.log('[Service Worker] Push Received.');
    // console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);
    //console.log('sw推送消息已经完成 推送回来的数据是',event.data.text())
    //console.log('检测数据类型',typeof(event.data.text()))
    var jsonData = JSON.parse(event.data.text())

    var title = jsonData.title;
    linkUrl = jsonData.url
    var options = {
        body: jsonData.message,
        icon: 'img/logo.png',
        badge: 'img/badge.png'
    };
    //自定义推送内容 展示推送栏消息
    event.waitUntil(self.registration.showNotification(title, options));
    _hmt.push(['_trackEvent', '推送消息弹框', '消息', '', '展示推送弹框'])
    // 下面是简写
    // const notificationPromise = self.registration.showNotification(title, options);
    // event.waitUntil(notificationPromise);
    // 轮询请求接口数据

});
// self.addEventListener("sync", function (event) {
//   if (event.tag == "workOrderSync") {
//     event.waitUntil(workOrderSync())
//   }
// });

// 点击推来的消息的某一条的监听
self.addEventListener('notificationclick', function (event) {
    // console.log('[Service Worker] Notification click Received.');
    // console.log('[Service Worker]点击其中一条推来的消息完成 关闭点过的通知 打开详情地址');
    event.notification.close();
    // 打开详情地址
    //console.log('linkurl===',linkUrl)
    // linkUrl = event.notification.data || ''
    // console.log('event notification====',event.notification)
    // console.log('action====sw',linkUrl)
    event.waitUntil(
        clients.openWindow(linkUrl));
    _hmt.push(['_trackEvent', '点击推送消息弹框', '点击消息弹框', '', '打开链接'])
    // event.waitUntil确保浏览器不会在显示新窗口前终止服务工作线程
});
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档