-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.js
48 lines (46 loc) · 1.33 KB
/
send.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
const SERVER_KEY = 'YOUR_SERVER_KEY';
console.assert(/^\w{11}:[\w-]{140}$/.test(SERVER_KEY));
(async () => {
let form = document.querySelector('form');
let input = document.querySelector('input');
let button = document.querySelector('button');
form.addEventListener('submit', async event => {
event.preventDefault();
button.disabled = true;
try {
let result = await fetch('https://fcm.googleapis.com/fcm/send', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `key=${SERVER_KEY}`
},
body: JSON.stringify({
data: {
foo: "foo is awsome!",
bar: "bar is awsome!",
},
to: input.value,
}),
}).then(response => response.json());
new Notification('message sent from browser action', {
body: JSON.stringify(result),
});
} catch (e) {
new Notification('message not sent from browser action', {
body: e
});
} finally {
button.disabled = false;
}
});
input.value = await new Promise((resolve, reject) => {
chrome.storage.local.get('token', data => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(data.token);
}
})
});
button.disabled = false;
})()