SHARE
TWEET

index.php

michussj07 Dec 28th, 2018 (edited) 11 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Push notification</title>
  5.     <script src="jquery-3.3.1.min.js"></script>
  6. <script src="https://www.gstatic.com/firebasejs/5.7.1/firebase.js"></script>
  7. <link rel="manifest" href="manifest.json">
  8. <script>
  9.   // Initialize Firebase
  10.   var config = {
  11.     apiKey: "AIzaSyDZ56QKkFT33hC3Ee3KyhqwkM0Pmkysg20",
  12.     authDomain: "push-v2-7a3d4.firebaseapp.com",
  13.     databaseURL: "https://push-v2-7a3d4.firebaseio.com",
  14.     projectId: "push-v2-7a3d4",
  15.     storageBucket: "",
  16.     messagingSenderId: "335978700030"
  17.   };
  18.   firebase.initializeApp(config);
  19.  
  20.   // Retrieve Firebase Messaging object.
  21.   const messaging = firebase.messaging();
  22.  
  23.   messaging.requestPermission().then(function() {
  24.       console.log('Notification permission granted.');
  25.       // TODO(developer): Retrieve an Instance ID token for use with FCM.
  26.    if(isTokenSentToServer()) {
  27.  console.log('Token already saved')
  28.    } else {
  29.        getRegToken();
  30.    }
  31.    //getRegToken();
  32.    }).catch(function(err) {
  33.    console.log('Unable to get permission to notify.', err);
  34. });
  35.  
  36. function getRegToken (argument) {
  37.        messaging.getToken().then(function(currentToken) {
  38.            if (currentToken) {
  39.                saveToken(currentToken);
  40.                console.log(currentToken);
  41.                setTokenSentToServer(true);
  42.            } else {
  43.                console.log('No Instance ID token available. Request permission to generate one.');
  44.                setTokenSentToServer(false);
  45.            }
  46.        }).catch(function(err) {
  47.            console.log('An error occurred while retrieving token. ', err);
  48.            setTokenSentToServer(false);
  49.        });
  50. }
  51.  
  52. function setTokenSentToServer(sent) {
  53.    window.localStorage.setItem('sentToServer', sent ? '1' : '0');
  54. }
  55.  
  56. function isTokenSentToServer() {
  57.    return window.localStorage.getItem('sentToServer') === '1';
  58. }
  59.  
  60.     function saveToken(currentToken) {
  61.         $.ajax({
  62.             url: 'action.php',
  63.             method: 'post',
  64.             data: 'token=' + currentToken
  65.  
  66.  
  67.         }).done(function(result){
  68.             console.log(result);
  69.         })
  70.     }
  71.  
  72. messaging.onMessage(function (payLoad) {
  73.     console.log('Message Received' + payLoad);
  74. });
  75.  
  76.  
  77.  
  78.  
  79.   </script>
  80.   </head>
  81.       <body>
  82.  
  83.       <h1>Push notification v3</h1>
  84.       </body>
  85.  
  86.  
  87. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top