firebase-messaging-sw.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. // this file must be placed in root
  2. importScripts('https://www.gstatic.com/firebasejs/4.6.1/firebase-app.js');
  3. importScripts('https://www.gstatic.com/firebasejs/4.6.1/firebase-messaging.js');
  4. firebase.initializeApp({
  5. messagingSenderId: '813343642322',
  6. });
  7. const messaging = firebase.messaging();
  8. self.addEventListener('push', function (event) {
  9. const payload = event.data.json() || {};
  10. const payloadNotification = payload.notification || {};
  11. const payloadData = payload.data || {};
  12. const options = {
  13. body: payloadNotification.body || payloadData.body,
  14. icon: payloadNotification.icon || payloadData.icon || '/img/icons/android-chrome-167x167.png',
  15. badge : '/img/icons/icon-messages-96x96.png',
  16. image : payloadNotification.image || payloadData.image ,
  17. vibrate : [300, 100, 400],
  18. data: payloadNotification.click_action || payloadData.click_action || payloadNotification.clickAction || payloadData.clickAction
  19. };
  20. event.waitUntil(self.registration.showNotification(payloadNotification.title, options));
  21. });
  22. self.addEventListener('notificationclick', function (event) {
  23. event.notification.close();
  24. event.waitUntil(clients.openWindow('/mobile/#' + (event.notification.data || '/home')));
  25. });