-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase-messaging-sw.js
155 lines (132 loc) · 4.71 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* eslint-disable func-names */
/* eslint-disable no-plusplus */
/* eslint-disable no-undef */
/* eslint-disable eqeqeq */
/* eslint-disable no-restricted-globals */
importScripts(
'https://www.gstatic.com/firebasejs/9.1.1/firebase-app-compat.js',
);
importScripts(
'https://www.gstatic.com/firebasejs/9.1.1/firebase-messaging-compat.js',
);
console.log('===========================================');
console.log('FIREBASE SERVICE WORKER RUNNING');
console.log('===========================================');
// Initialize the Firebase app in the service worker by passing in
// your app's Firebase config object.
// https://firebase.google.com/docs/web/setup#config-object
firebase.initializeApp({
apiKey: 'AIzaSyDBhaYgqcyctFSX_LDUA8AzAaBIx1csvBE',
authDomain: 'sehatq-testing.firebaseapp.com',
projectId: 'sehatq-testing',
storageBucket: 'sehatq-testing.appspot.com',
messagingSenderId: '644611337350',
appId: '1:644611337350:web:bbe5aefd8f0997bb7e3507',
});
/** START CUSTOM PUSH EVENT */
class CustomPushEvent extends Event {
constructor(data) {
super('push');
Object.assign(this, data);
this.custom = true;
}
}
/*
* Overrides push notification data, to avoid having 'notification' key and firebase blocking
* the message handler from being called
*/
self.addEventListener('push', (e) => {
// Skip if event is our own custom event
if (e.custom) return;
// Kep old event data to override
const oldData = e.data;
// Create a new event to dispatch, pull values from notification key and put it in data key,
// and then remove notification key
const newEvent = new CustomPushEvent({
data: {
ehheh: oldData.json(),
json() {
const newData = oldData.json();
newData.data = {
...newData.data,
...newData.notification,
};
delete newData.notification;
return newData;
},
},
waitUntil: e.waitUntil.bind(e),
});
// Stop event propagation
e.stopImmediatePropagation();
// Dispatch the new wrapped event
dispatchEvent(newEvent);
});
/** END CUSTOM PUSH EVENT */
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
/** START custom onnotificationclick handler */
self.onnotificationclick = function (event) {
console.log('On notification click: ', event.notification.tag);
console.log('event.notification: ', event.notification);
event.notification.close();
const pathAction = '/';
const actionUrl = `${location.origin}${pathAction}`;
/** FOR CUSTOM click_action / if there's a custom click_action (url) data */
if (event.notification.data && event.notification.data.click_action) {
self.clients.openWindow(event.notification.data.click_action);
return;
}
/** end custom click_action */
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(
clients
.matchAll({
includeUncontrolled: true, // IMPORTANT: include the uncontrolled window
type: 'window',
})
// eslint-disable-next-line func-names
// eslint-disable-next-line consistent-return
.then(function (clientList) {
console.log('clientList', clientList);
for (let i = 0; i < clientList.length; i++) {
const client = clientList[i];
if (client.url == actionUrl && 'focus' in client)
return client.focus();
}
if (clients.openWindow) return clients.openWindow(actionUrl);
}),
);
};
/** END custom onnotificationclick handler */
/** START receive onBackgroundMessage handler */
// If you would like to customize notifications that are received in the
// background (Web app is closed or not in browser focus) then you should
// implement this optional method.
// Keep in mind that FCM will still show notification messages automatically
// and you should use data messages for custom notifications.
// For more info see:
// https://firebase.google.com/docs/cloud-messaging/concept-options
messaging.onBackgroundMessage(function (payload) {
if (!payload && !payload.data) {
console.error('payload message notification data is empty!');
return;
}
console.log(
'[firebase-messaging-sw.js] Received background message ',
payload,
);
// Customize notification here
const notificationTitle = payload.data.title || 'Notif';
const notificationOptions = {
body: payload.data.body || 'Body Notification',
icon: payload.data.icon || '/favicon.ico',
badge: payload.data.badge || '/favicon.ico',
tag: payload.data.tag || 'main',
sound: payload.data.sound || 'default',
};
self.registration.showNotification(notificationTitle, notificationOptions);
});
/** END receive onBackgroundMessage handler */