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

0
(0)

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


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

/*
 *
 *  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确保浏览器不会在显示新窗口前终止服务工作线程
});

这篇文章有用吗?

平均评分 0 / 5. 投票数: 0

到目前为止还没有投票!成为第一位评论此文章。

很抱歉,这篇文章对您没有用!

让我们改善这篇文章!

告诉我们我们如何改善这篇文章?

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据