najimCseJu

Untitled

Feb 5th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. ******************** SDK set up and foreground message handling. ***************
  2.  
  3.  
  4. <script>
  5. // Initialize Firebase
  6. var config = {
  7. apiKey: "AIzaSyCpxieXvbvbUuQy9vFQFX1ZxvVKyID2chY",
  8. authDomain: "detroitechcallcenterapp.firebaseapp.com",
  9. databaseURL: "https://detroitechcallcenterapp.firebaseio.com",
  10. projectId: "detroitechcallcenterapp",
  11. storageBucket: "detroitechcallcenterapp.appspot.com",
  12. messagingSenderId: "94883233124"
  13. };
  14. firebase.initializeApp(config);
  15. const messaging = firebase.messaging();
  16. messaging.requestPermission().then(function () {
  17. console.log("Permission given");
  18. return messaging.getToken();
  19. }, function () {
  20. console.log("Permission dennied")
  21. }).then(function (token) {
  22. console.log("my token", token);
  23. });
  24. messaging.onMessage(function (payload) {
  25. console.log('Message received :: online. ', payload);
  26. // ...
  27. });
  28. </script>
  29.  
  30.  
  31. ******************** firebase-messaging-sw.js file in the root directory to handle background notification***************
  32. // Give the service worker access to Firebase Messaging.
  33. // Note that you can only use Firebase Messaging here, other Firebase libraries
  34. // are not available in the service worker.
  35. importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-app.js');
  36. importScripts('https://www.gstatic.com/firebasejs/4.8.1/firebase-messaging.js');
  37.  
  38. // Initialize the Firebase app in the service worker by passing in the
  39. // messagingSenderId.
  40. firebase.initializeApp({
  41. 'messagingSenderId': '94883233124'
  42. });
  43.  
  44. // Retrieve an instance of Firebase Messaging so that it can handle background
  45. // messages.
  46. const messaging = firebase.messaging();
  47.  
  48.  
  49. // Handle incoming messages. Called when:
  50. // - a message is received while the app has focus
  51. // - the user clicks on an app notification created by a service worker
  52. // `messaging.setBackgroundMessageHandler` handler.
  53. messaging.setBackgroundMessageHandler(function(payload){
  54. const title = "Message from detroitech";
  55. const options = {
  56. body : payload.data.testMessage
  57. };
  58. return self.registration.showNotification(title,options);
  59. });
  60. messaging.setBackgroundMessageHandler(function (payload) {
  61. console.log('Handling background message ', payload);
  62.  
  63. return self.registration.showNotification(payload.data.title, {
  64. body: payload.data.body,
  65. icon: payload.data.icon,
  66. tag: payload.data.tag,
  67. data: payload.data.link
  68. });
  69. });
  70.  
  71. self.addEventListener('notificationclick', function(event) {
  72. event.notification.close();
  73. event.waitUntil(self.clients.openWindow(event.notification.data));
  74. });
Add Comment
Please, Sign In to add comment