发表于2018-12-252019-04-28 作者 wind
今天发现CSDN通过浏览器推送了好多条消息
点击进入后是这么一个内容的js,看起来就是实现推送的代码,留着,以后可能会用到的~
代码语言:javascript复制/*
*
* 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确保浏览器不会在显示新窗口前终止服务工作线程
});