-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
61 lines (58 loc) · 1.46 KB
/
background.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
consoleLog('开始执行background.js');
init();
/**
* 应用初始化
*/
function init() {
initStatus();
listenNotificationClicked();
chrome.runtime.onInstalled.addListener((e) => {
if (e.reason === chrome.runtime.OnInstalledReason.INSTALL) {
chrome.tabs.create({
'url': 'popup.html'
});
chrome.runtime.setUninstallURL('https://wxpusher.zjiecode.com/docs/#/');
}
});
initWsConnect();
}
function initStatus(){
var version = chrome.runtime.getManifest().version;
setVersion(version);
//打开的时候,长链接没有打开
setWsConnect(false);
}
/**
* 建立ws链接
*/
function initWsConnect() {
wsConnect(function (wsMsg) {
if (wsMsg.msgType === WS_MSG_TYPE_UPSH_NOTIFICATION) {
showNotification('WxPusher通知提醒',wsMsg.content,wsMsg.qid);
return
}
if (wsMsg.msgType === WS_MSG_TYPE_INIT) {
setPushToken(wsMsg.pushToken);
initOrUpdatePushToken(wsMsg);
return
}
});
}
/**
* 初始化设备,或者更新设别pushToken
*/
function initOrUpdatePushToken(wsInitMsg) {
var deviceUuid = getDeviceUuid();
var resultCallback = function (resp) {
setDeviceUuid(resp.deviceUuid);
setDeviceToken(resp.deviceToken);
}
var errorCallback = function (e) {
//进行错误提示
}
if (deviceUuid) {
updatePushToken(wsInitMsg.pushToken, deviceUuid, resultCallback, errorCallback);
return;
}
//如果没有uid,需要用户点开扩展弹出页面,扫码登陆
}