|
| 1 | +<!DOCTYPE html> |
| 2 | +<!-- |
| 3 | +Copyright (c) 2016 Google Inc. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +--> |
| 17 | +<html> |
| 18 | +<head> |
| 19 | + <meta charset=utf-8/> |
| 20 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 21 | + <title>Firebase Cloud Messaging Example</title> |
| 22 | + |
| 23 | + <!-- Material Design Theming --> |
| 24 | + <link rel="stylesheet" href="https://code.getmdl.io/1.1.3/material.orange-indigo.min.css"> |
| 25 | + <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons"> |
| 26 | + <script defer src="https://code.getmdl.io/1.1.3/material.min.js"></script> |
| 27 | + |
| 28 | + <link rel="stylesheet" href="main.css"> |
| 29 | +</head> |
| 30 | +<body> |
| 31 | +<div class="demo-layout mdl-layout mdl-js-layout mdl-layout--fixed-header"> |
| 32 | + |
| 33 | + <!-- Header section containing title --> |
| 34 | + <header class="mdl-layout__header mdl-color-text--white mdl-color--light-blue-700"> |
| 35 | + <div class="mdl-cell mdl-cell--12-col mdl-cell--12-col-tablet mdl-grid"> |
| 36 | + <div class="mdl-layout__header-row mdl-cell mdl-cell--12-col mdl-cell--12-col-tablet mdl-cell--8-col-desktop"> |
| 37 | + <h3>Firebase Cloud Messaging</h3> |
| 38 | + </div> |
| 39 | + </div> |
| 40 | + </header> |
| 41 | + |
| 42 | + <main class="mdl-layout__content mdl-color--grey-100"> |
| 43 | + <div class="mdl-cell mdl-cell--12-col mdl-cell--12-col-tablet mdl-grid"> |
| 44 | + |
| 45 | + <!-- Container for the Table of content --> |
| 46 | + <div class="mdl-card mdl-shadow--2dp mdl-cell mdl-cell--12-col mdl-cell--12-col-tablet mdl-cell--12-col-desktop"> |
| 47 | + <div class="mdl-card__supporting-text mdl-color-text--grey-600"> |
| 48 | + <div id="token_div" style="display: none;"> |
| 49 | + <h4>Token</h4> |
| 50 | + <p id="token" style="word-break: break-all;"></p> |
| 51 | + <button class="mdl-button mdl-js-button mdl-button--raised mdl-button--colored" |
| 52 | + onclick="deleteToken()">Delete Token</button> |
| 53 | + </div> |
| 54 | + <div id="permission_div" style="display: none;"> |
| 55 | + <h4>Needs Permission</h4> |
| 56 | + <p id="token"></p> |
| 57 | + <button class="mdl-button mdl-js-button mdl-button--raised mdl-button--colored" |
| 58 | + onclick="askPermission()">Request Permission</button> |
| 59 | + </div> |
| 60 | + <div id="messages"></div> |
| 61 | + </div> |
| 62 | + </div> |
| 63 | + |
| 64 | + </div> |
| 65 | + </main> |
| 66 | +</div> |
| 67 | +<!-- Firebase --> |
| 68 | +<!-- ******************************************************************************* |
| 69 | + * TODO(DEVELOPER): Paste the initialization snippet by navigating to: |
| 70 | + https://console.firebase.google.com |
| 71 | + and choosing a project you've created. Then click the red HTML logo at the top |
| 72 | + right of the page with the caption "Add Firebase to your web app". |
| 73 | + Copy the snippet that appears in place of this comment. |
| 74 | + *************************************************************************** --> |
| 75 | +<script> |
| 76 | + // [START get_messaging_object] |
| 77 | + const messaging = firebase.messaging(); |
| 78 | + // [END get_messaging_object] |
| 79 | + const tokenDivId = 'token_div'; |
| 80 | + const permissionDivId = 'permission_div'; |
| 81 | + |
| 82 | + // [START refresh_token] |
| 83 | + // Called if InstanceID token is updated. This may occur if the security of |
| 84 | + // the previous token had been compromised. Note that you will have to |
| 85 | + // re-subscribe the new token to the topics that the previous token was |
| 86 | + // subscribed to. |
| 87 | + messaging.onTokenRefresh(function() { |
| 88 | + messaging.getToken() |
| 89 | + .then(function(refreshedToken) { |
| 90 | + console.log('Token refreshed.'); |
| 91 | + setTokenSentToServer(false); |
| 92 | + sendTokenToServer(refreshedToken); |
| 93 | + resetUI(); |
| 94 | + }); |
| 95 | + }); |
| 96 | + // [END refresh_token] |
| 97 | + |
| 98 | + // [START receive_message] |
| 99 | + // Handle messages that are received by this page. This is called when a |
| 100 | + // message is received while the app is in the foreground or when the user |
| 101 | + // clicks on a the notification resulting from the user clicking on a |
| 102 | + // notification message. |
| 103 | + messaging.onMessage(function(payload) { |
| 104 | + console.log("Message received."); |
| 105 | + console.log(payload); |
| 106 | + // [START_EXCLUDE] |
| 107 | + appendMessage(payload); |
| 108 | + // [END_EXCLUDE] |
| 109 | + }); |
| 110 | + // [END receive_message] |
| 111 | + |
| 112 | + function resetUI() { |
| 113 | + // Reset UI |
| 114 | + clearMessages(); |
| 115 | + showToken('loading...'); |
| 116 | + // Get Instance ID token. Initially this makes a network call, once retrieved |
| 117 | + // subsequent calls to getToken will return from cache. |
| 118 | + // [START get_token] |
| 119 | + messaging.getToken() |
| 120 | + .then(function(currentToken) { |
| 121 | + if (currentToken) { |
| 122 | + sendTokenToServer(currentToken); |
| 123 | + // Update UI to show token. |
| 124 | + updateUIForPushEnabled(currentToken); |
| 125 | + } else { |
| 126 | + // Show permission request. |
| 127 | + console.log('No Instance ID token available. Request permission to generate one.'); |
| 128 | + // Show permission UI. |
| 129 | + updateUIForPushPermissionRequired(); |
| 130 | + setTokenSentToServer(false); |
| 131 | + } |
| 132 | + }) |
| 133 | + .catch(function(err) { |
| 134 | + console.log('An error occurred while retrieving token. This may occurr after ' + |
| 135 | + 'notification permission has been denied. ', err); |
| 136 | + setTokenSentToServer(false); |
| 137 | + }); |
| 138 | + } |
| 139 | + // [END get_token] |
| 140 | + resetUI(); |
| 141 | + |
| 142 | + function showToken(currentToken) { |
| 143 | + // Show token in console and UI. |
| 144 | + var tokenEle = document.querySelector('#token'); |
| 145 | + tokenEle.textContent = currentToken; |
| 146 | + } |
| 147 | + |
| 148 | + // Send the Instance ID token your application server so that you can use it |
| 149 | + // to send message to this app. Also you can use the token on the application |
| 150 | + // server to subscribe/unsubscribe the token from topics. |
| 151 | + function sendTokenToServer(currentToken) { |
| 152 | + if (!isTokenSentToServer()) { |
| 153 | + console.log('Sending token to server...'); |
| 154 | + // TODO(developer): Send the current token to your server. |
| 155 | + setTokenSentToServer(true); |
| 156 | + } else { |
| 157 | + console.log('Token already sent to server so won\'t send it again ' + |
| 158 | + 'unless it changes'); |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + function isTokenSentToServer() { |
| 164 | + if (window.localStorage.getItem('sentToServer') == 1) { |
| 165 | + return true; |
| 166 | + } |
| 167 | + return false; |
| 168 | + } |
| 169 | + |
| 170 | + function setTokenSentToServer(sent) { |
| 171 | + if (sent) { |
| 172 | + window.localStorage.setItem('sentToServer', 1); |
| 173 | + } else { |
| 174 | + window.localStorage.setItem('sentToServer', 0); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + function showHideDiv(divId, show) { |
| 179 | + const div = document.querySelector('#' + divId); |
| 180 | + if (show) { |
| 181 | + div.style = "display: visible"; |
| 182 | + } else { |
| 183 | + div.style = "display: none"; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + function askPermission() { |
| 188 | + console.log('Requesting permission...'); |
| 189 | + // [START request_permission] |
| 190 | + messaging.requestPermission() |
| 191 | + .then(function() { |
| 192 | + console.log('Notification permission granted.'); |
| 193 | + // [START_EXCLUDE] |
| 194 | + // Now you can retrieve a Instance ID token for use with FCM. |
| 195 | + resetUI(); |
| 196 | + // [END_EXCLUDE] |
| 197 | + }) |
| 198 | + .catch(function(err) { |
| 199 | + console.log('Unable to get permission to notify.'); |
| 200 | + }); |
| 201 | + // [END request_permission] |
| 202 | + } |
| 203 | + |
| 204 | + function deleteToken() { |
| 205 | + // Delete Instance ID token. |
| 206 | + // [START delete_token] |
| 207 | + messaging.deleteToken() |
| 208 | + .then(function() { |
| 209 | + console.log('Token deleted.'); |
| 210 | + setTokenSentToServer(false); |
| 211 | + // [START_EXCLUDE] |
| 212 | + // Once token is deleted update UI. |
| 213 | + resetUI(); |
| 214 | + // [END_EXCLUDE] |
| 215 | + }) |
| 216 | + .catch(function(err) { |
| 217 | + console.log('Unable to delete token.'); |
| 218 | + }); |
| 219 | + // [END delete_token] |
| 220 | + } |
| 221 | + |
| 222 | + // Add a messages to the messages element. |
| 223 | + function appendMessage(payload) { |
| 224 | + const messagesElement = document.querySelector('#messages'); |
| 225 | + const dataHeaderELement = document.createElement('h5'); |
| 226 | + const dataElement = document.createElement('p'); |
| 227 | + dataHeaderELement.textContent = 'Received message:'; |
| 228 | + dataElement.textContent = JSON.stringify(payload.data); |
| 229 | + messagesElement.appendChild(dataHeaderELement); |
| 230 | + messagesElement.appendChild(dataElement); |
| 231 | + } |
| 232 | + |
| 233 | + // Clear the messages element of all children. |
| 234 | + function clearMessages() { |
| 235 | + const messagesElement = document.querySelector('#messages'); |
| 236 | + while (messagesElement.hasChildNodes()) { |
| 237 | + messagesElement.removeChild(messagesElement.lastChild); |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + function updateUIForPushEnabled(currentToken) { |
| 242 | + showHideDiv(tokenDivId, true); |
| 243 | + showHideDiv(permissionDivId, false); |
| 244 | + showToken(currentToken); |
| 245 | + } |
| 246 | + |
| 247 | + function updateUIForPushPermissionRequired() { |
| 248 | + showHideDiv(tokenDivId, false); |
| 249 | + showHideDiv(permissionDivId, true); |
| 250 | + } |
| 251 | +</script> |
| 252 | +</body> |
| 253 | +</html> |
0 commit comments