-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase-messaging-sw.js
46 lines (35 loc) · 1 KB
/
firebase-messaging-sw.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
importScripts('https://www.gstatic.com/firebasejs/7.14.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.14.0/firebase-messaging.js');
let config = {}
this.addEventListener('install', (event) => {
parseQueryToObj(event.srcElement.location.search)
validateConfig() ? init() : null
})
/**
* @method parseQueryToObj
* @param { String } query
*/
function parseQueryToObj(query) {
query = query.slice(1).split('&')
for (let i = 0; i < query.length; i++) {
const [key, value] = query[i].split('=')
config[key] = value
}
}
function init() {
console.log(config)
firebase.initializeApp(config)
const messaging = firebase.messaging()
messaging.setBackgroundMessageHandler((payload) => {
console.log('[firebase-messaging-sw] Received background message', payload)
return self.registration.showNotification('Test', {
body: 'Test'
})
})
}
function validateConfig() {
for (const key in config) {
if (config[key] === '') return false
}
return true
}