PageSourceSearch

https://desapi.com/plugins/PushNotifications/messaging/firebase-web.js

js desapi.com collected 2026-10-09 01:52:25 UTC 7,219 bytes, 224 lines download raw bytes

1// Initialize Firebase
2  var config = {
3    apiKey: "AIzaSyDeCbxU3MckTbYOQPxwc34E8RX8QlX2vXI",
4    authDomain: "earthway-c197e.firebaseapp.com",
5    databaseURL: "https://earthway-c197e.firebaseio.com",
6    projectId: "earthway-c197e",
7    storageBucket: "earthway-c197e.appspot.com",
8    messagingSenderId: "246290232104"
9  };
10
11  firebase.initializeApp(config);
12  
13  var messaging = null;
14  
15  function printLog(logmsg){
16	  console.log(logmsg);
17  }
18  
19  function printLogs(logmsg, obj){
20	  console.log(logmsg, obj);
21  }
22  
23  /*navigator.serviceWorker.register('/PushNotifications/messaging/firebase-messaging-sw.js')
24      .then(function (registration) {
25	  messaging = firebase.messaging();
26	  messaging.usePublicVapidKey('BE4rP5wr4s2eTLbC-r_2DLhv9vV2Qg1o_EtSNquWdjwc3tYAFi6iYLsLXsoMLOPws4x6q2uTxxJsVGWKlHaSv18');
27	  requestPermission();
28	  configMessaging();
29      })*/
30	  
31  // [START get_messaging_object]
32  // Retrieve Firebase Messaging object.
33  messaging = firebase.messaging();
34  // [END get_messaging_object]
35  // [START set_public_vapid_key]
36  // Add the public key generated from the console here.
37  messaging.usePublicVapidKey('BE4rP5wr4s2eTLbC-r_2DLhv9vV2Qg1o_EtSNquWdjwc3tYAFi6iYLsLXsoMLOPws4x6q2uTxxJsVGWKlHaSv18');
38  // [END set_public_vapid_key]
39  requestPermission();
40  configMessaging();
41  
42  function configMessaging(){
43  // [START refresh_token]
44  // Callback fired if Instance ID token is updated.
45  messaging.onTokenRefresh(function() {
46    messaging.getToken().then(function(refreshedToken) {
47      printLog('Token refreshed.');
48      // Indicate that the new Instance ID token has not yet been sent to the
49      // app server.
50      setTokenSentToServer(false);
51      // Send Instance ID token to app server.
52      sendTokenToServer(refreshedToken);
53      // [START_EXCLUDE]
54      // Display new Instance ID token and clear UI of all previous messages.
55      resetUI();
56      // [END_EXCLUDE]
57    }).catch(function(err) {
58      printLogs('Unable to retrieve refreshed token ', err);
59    });
60  });
61  // [END refresh_token]
62
63  // [START receive_message]
64  // Handle incoming messages. Called when:
65  // - a message is received while the app has focus
66  // - the user clicks on an app notification created by a service worker
67  //   `messaging.setBackgroundMessageHandler` handler.
68  messaging.onMessage(function(payload) {
69    printLogs('Message received. ', payload);
70    // [START_EXCLUDE]
71	
72	var notificationTitle = payload.data.title;
73  var notificationOptions = {
74    body: payload.data.body,
75    icon: payload.data.icon,
76	vibrate: [200, 100, 200, 100, 200, 100, 200],
77	data: payload.data.link,
78	tag: "not"
79  };
80
81	var notification = new Notification(notificationTitle, notificationOptions);
82	
83	notification.onclick = function(event) {
84	  event.preventDefault(); // prevent the browser from focusing the Notification's tab
85	  if(payload.data.link != ""){
86		window.open(payload.data.link, '_blank');
87	  }
88	}
89    // [END_EXCLUDE]
90  });
91  // [END receive_message]
92  resetUI();
93} // End configMessaging()
94
95  function resetUI() {
96    // [START get_token]
97    // Get Instance ID token. Initially this makes a network call, once retrieved
98    // subsequent calls to getToken will return from cache.
99    messaging.getToken().then(function(currentToken) {
100      if (currentToken) {
101		printLog('currentToken:' + currentToken);
102        sendTokenToServer(currentToken);
103      } else {
104        // Show permission request.
105        printLog('No Instance ID token available. Request permission to generate one.');
106        setTokenSentToServer(false);
107      }
108    }).catch(function(err) {
109      printLogs('An error occurred while retrieving token. ', err);
110      setTokenSentToServer(false);
111    });
112    // [END get_token]
113  }
114
115
116  // Send the Instance ID token your application server, so that it can:
117  // - send messages back to this app
118  // - subscribe/unsubscribe the token from topics
119  function sendTokenToServer(currentToken) {
120  printLog(currentToken);
121    if (!isTokenSentToServer() || currentToken != getLocalToken()) {
122      printLog('Sending token to server...');
123	  saveToken(currentToken);
124      //setTokenSentToServer(true);
125    } else {
126      printLog('Token already sent to server so won\'t send it again ' +
127          'unless it changes');
128    }
129
130  }
131  
132  function saveToken(currentToken){
133		$.ajax({
134			url: '/plugins/PushNotifications/SaveToken.aspx',
135			method: 'post',
136			data: 'token=' + currentToken
137			,
138			cache: false,
139    success: function( data, textStatus, jqXHR ) {
140        printLogs("success:", data);
141		saveLocalToken(currentToken);
142		setTokenSentToServer(true);
143    },
144    error: function(jqXHR, exception)
145    {
146		printLog("status:"+jqXHR.status+",,, ex:"+exception+",,, res:"+jqXHR.responseText);
147		if(jqXHR.status != 200){
148			setTokenSentToServer(false);
149		}
150        /*if (jqXHR.status === 0) {
151            alert('Not connect.\n Verify Network.');
152        } else if (jqXHR.status == 404) {
153            alert('Requested page not found. [404]');
154        } else if (jqXHR.status == 500) {
155            alert('Internal Server Error [500].');
156        } else if (exception === 'parsererror') {
157            alert('Requested JSON parse failed.');
158        } else if (exception === 'timeout') {
159            alert('Time out error.');
160        } else if (exception === 'abort') {
161            alert('Ajax request aborted.');
162        } else {
163            alert('Uncaught Error.\n' + jqXHR.responseText);
164        }*/
165    }
166		}).done(function(result){
167			printLog("done:"+result);
168			saveLocalToken(currentToken);
169			setTokenSentToServer(true);
170		})
171  }
172
173  function isTokenSentToServer() {
174    return window.localStorage.getItem('sentToServer') === '1';
175  }
176  function setTokenSentToServer(sent) {
177    window.localStorage.setItem('sentToServer', sent ? '1' : '0');
178  }
179  
180  function getLocalToken() {
181    return window.localStorage.getItem('localToken');
182  }
183  function saveLocalToken(token) {
184    window.localStorage.setItem('localToken', token);
185  }
186
187  function requestPermission() {
188    printLog('Requesting permission...');
189    // [START request_permission]
190    messaging.requestPermission().then(function() {
191      printLog('Notification permission granted.');
192      // TODO(developer): Retrieve an Instance ID token for use with FCM.
193      // [START_EXCLUDE]
194      // In many cases once an app has been granted notification permission, it
195      // should update its UI reflecting this.
196      resetUI();
197	  	
198      // [END_EXCLUDE]
199    }).catch(function(err) {
200      printLogs('Unable to get permission to notify.', err);
201    });
202    // [END request_permission]
203  }
204
205  function deleteToken() {
206    // Delete Instance ID token.
207    // [START delete_token]
208    messaging.getToken().then(function(currentToken) {
209      messaging.deleteToken(currentToken).then(function() {
210        printLog('Token deleted.');
211        setTokenSentToServer(false);
212        // [START_EXCLUDE]
213        // Once token is deleted update UI.
214        resetUI();
215        // [END_EXCLUDE]
216      }).catch(function(err) {
217        printLogs('Unable to delete token. ', err);
218      });
219      // [END delete_token]
220    }).catch(function(err) {
221      printLogs('Error retrieving Instance ID token. ', err);
222    });
223
224  }

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.