Advertisement
Guest User

index.html

a guest
Dec 16th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. <html>
  2. <title>Firebase Messaging Demo</title>
  3. <style>
  4. div {
  5. margin-bottom: 15px;
  6. }
  7. </style>
  8. <body>
  9. <div id="token"></div>
  10. <div id="msg"></div>
  11. <div id="notis"></div>
  12. <div id="err"></div>
  13. <script src="https://www.gstatic.com/firebasejs/7.6.0/firebase-app.js"></script>
  14. <script src="https://www.gstatic.com/firebasejs/7.6.0/firebase-messaging.js"></script>
  15. <!-- For an optimal experience using Cloud Messaging, also add the Firebase SDK for Analytics. -->
  16. <script src="https://www.gstatic.com/firebasejs/7.6.0/firebase-analytics.js"></script>
  17. <script src="https://www.gstatic.com/firebasejs/7.6.0/firebase-auth.js"></script>
  18. <script src="https://www.gstatic.com/firebasejs/7.6.0/firebase-firestore.js"></script>
  19. <script>
  20. MsgElem = document.getElementById("msg");
  21. TokenElem = document.getElementById("token");
  22. NotisElem = document.getElementById("notis");
  23. ErrElem = document.getElementById("err");
  24. // Initialize Firebase
  25. // TODO: Replace with your project's customized code snippet
  26. const firebaseConfig = {
  27. apiKey: "AIzaSyAS6TCYSTEJhLZ0gkFEvK9fbVylVxvlJqs",
  28. authDomain: "pushnotification-ac7c2.firebaseapp.com",
  29. databaseURL: "https://pushnotification-ac7c2.firebaseio.com",
  30. projectId: "pushnotification-ac7c2",
  31. storageBucket: "pushnotification-ac7c2.appspot.com",
  32. messagingSenderId: "468475721690",
  33. appId: "1:468475721690:web:2981ddcfe6580e894c72e3"
  34. };
  35. firebase.initializeApp(firebaseConfig);
  36. const messaging = firebase.messaging();
  37. messaging
  38. .requestPermission()
  39. .then(function() {
  40. MsgElem.innerHTML = "Notification permission granted.";
  41. console.log("Notification permission granted.");
  42.  
  43. // get the token in the form of promise
  44. return messaging.getToken();
  45. })
  46. .then(function(token) {
  47. TokenElem.innerHTML = "token is : " + token;
  48. })
  49. .catch(function(err) {
  50. ErrElem.innerHTML = ErrElem.innerHTML + "; " + err;
  51. console.log("Unable to get permission to notify.", err);
  52. });
  53. messaging.onMessage(function(payload) {
  54. console.log("Message received. ", payload);
  55. NotisElem.innerHTML = NotisElem.innerHTML + JSON.stringify(payload);
  56. //kenng - foreground notifications
  57. const { title, ...options } = payload.notification;
  58. navigator.serviceWorker.ready.then(registration => {
  59. registration.showNotification(title, options);
  60. });
  61. });
  62. </script>
  63. </body>
  64. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement