-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (128 loc) · 4.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var HOST = 'http://localhost:4000'
var messageBox = $('#message-online')
var isSubscribed;
function isEmptyObject(o) {
for (let i in o) {
return !1;
}
return !0;
}
function printInPage(msg){
var nowTime = new Date().toLocaleString()
var preStr = '<p>'+nowTime+'</p>'
messageBox.append(preStr+'<p>'+msg+'</p>')
}
function checkFeatures(){
if (!('serviceWorker' in navigator)) {
return "Service Worker isn't supported on this browser"
}
if (!('PushManager' in window)) {
return "Push isn't supported on this browser"
}
return true
}
function savePushSubscription(pushSubscription){
$.ajax({
type:"POST",
url: HOST+'/savePushSubscription',
data: {
"body": JSON.stringify(pushSubscription)
},
success: function(res){
printInPage('PushSubscription信息上传成功')
},
dataType: "json"
})
}
function getSWRegistration() {
return navigator.serviceWorker.register('/service-worker.js')
.then(function(SWRegistration) {
printInPage('Service worker 注册成功');
return SWRegistration;
})
.catch(function(err) {
printInPage('service worker 注册失败: '+err);
});
}
function urlBase64ToUint8Array(base64String) {
const padding = '='.repeat((4 - base64String.length % 4) % 4);
const base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
const rawData = window.atob(base64);
return Uint8Array.from([...rawData].map((char) => char.charCodeAt(0)));
}
function checkSubscription(){
return getSWRegistration()
.then(function(registration) {
return registration.pushManager.getSubscription()
})
}
// https://web-push-codelab.appspot.com/?hl=zh-cn
// 私钥:Jck1C1WJQQ4teQb5cdjGTKwNQyz3OKS_tE594bdwYpU
//为了用户安全, 每次收到推送的消息,都需要展示通知
function subscribePush(){
return getSWRegistration()
.then(function(SWRegistration){
const subscribeOptions = {
userVisibleOnly: true,
applicationServerKey: urlBase64ToUint8Array('BL9Q2UwJPt9MzYm0urv4GeVU0-34zpuD30VUbNH5hRNPqV-LwbDO3sJejdwlmr7nyGZ8dL3TBuW-vXsWFkjdSsc')
};
return SWRegistration.pushManager.subscribe(subscribeOptions)
})
.then(function(pushSubscription){
printInPage('初次订阅push service成功: '+JSON.stringify(pushSubscription));
return pushSubscription;
})
.catch(function(err){
printInPage('初次订阅push service失败: '+ JSON.stringify(err) );
})
}
function subscribeUserToPush() {
checkSubscription().then(function(subscription){
isSubscribed = !(subscription === null);
if(isSubscribed){
printInPage('push service 已经订阅');
return subscription
}
return subscribePush()
})
.then(function(pushSubscription){
if(!isEmptyObject(pushSubscription)) savePushSubscription(pushSubscription)
printInPage('PushSubscription信息: '+JSON.stringify(pushSubscription));
})
}
window.clickHandler = function(){
if(checkFeatures()===true) {
subscribeUserToPush()
}else {
printInPage(checkFeatures())
}
}
window.getBackInfo = function(){
$.ajax({
type:"GET",
url: HOST+'/getPushSubscription',
success: function(res){
printInPage('检测后台保存的pushSubscription:'+JSON.stringify(res))
}
})
}
window.sendMsg = function(){
var message = $('#js-msg-input').val().trim()
if(message.length===0) {
alert('请输入内容');
return;
}
$.ajax({
type: 'POST',
url: HOST+'/sendPushMessage',
data: {
message: message
},
dataType: 'json',
success: function(res){
printInPage('已经推送的消息:'+JSON.stringify(res))
}
})
}