firebase-messaging-sw.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. importScripts('https://www.gstatic.com/firebasejs/4.3.1/firebase-app.js');
  2. importScripts('https://www.gstatic.com/firebasejs/4.3.1/firebase-messaging.js');
  3. const msg_sender_id = '868637702480';
  4. firebase.initializeApp({
  5. 'messagingSenderId': msg_sender_id
  6. });
  7. const messaging = firebase.messaging();
  8. messaging.setBackgroundMessageHandler(function (message) {
  9. self.clients.matchAll({
  10. type: 'window',
  11. includeUncontrolled: true
  12. }).then(function (clients) {
  13. clients.forEach(function (client) {
  14. client.postMessage({
  15. "firebase-messaging-msg-type": "push-msg-received",
  16. "firebase-messaging-msg-data": message
  17. });
  18. })
  19. });
  20. if (message.from === msg_sender_id) {
  21. var title, body,
  22. icon = 'images/xabber-logo-48.png';
  23. var payload;
  24. try {
  25. payload = JSON.parse(atob(message.data.body));
  26. } catch (e) {
  27. payload = message.data
  28. }
  29. if (payload.action === 'settings_updated') {
  30. title = 'Settings updated';
  31. body = 'for user ' + payload.username;
  32. return self.registration.showNotification(title, {
  33. body: body,
  34. icon: icon
  35. });
  36. }
  37. }
  38. });
  39. self.addEventListener('notificationclick', function (event) {
  40. event.notification.close();
  41. });