-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceive.js
47 lines (45 loc) · 1.18 KB
/
receive.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
const SENDER_ID = 'YOUR_SENDER_ID';
console.assert(/^\d{12}$/.test(SENDER_ID));
new Notification('Event page loaded');
const refreshToken = async () => {
const tokenParams = {
authorizedEntity: SENDER_ID,
scope: 'GCM'
};
let token = await new Promise((resolve, reject) => {
let r = chrome.instanceID.getToken(tokenParams, token => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(token);
}
});
});
await new Promise((resolve, reject) => {
chrome.storage.local.set({ token }, () => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve();
}
})
});
new Notification('token', {
body: token,
});
};
chrome.runtime.onInstalled.addListener(details => {
new Notification('"onInstalled" event fired', {
body: JSON.stringify(details),
});
refreshToken();
});
chrome.instanceID.onTokenRefresh.addListener(() => {
new Notification('"onTokenRefresh" event fired');
refreshToken();
});
chrome.gcm.onMessage.addListener(message => {
new Notification('"onMessage" event fired', {
body: JSON.stringify(message),
});
});