|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 6 | + <title>Example: TalkJS with Firebase Cloud Messaging notifications</title> |
| 7 | + |
| 8 | + <script> |
| 9 | + (function (t, a, l, k, j, s) { |
| 10 | + s = a.createElement("script"); |
| 11 | + s.async = 1; |
| 12 | + s.src = "https://cdn.talkjs.com/talk.js"; |
| 13 | + a.head.appendChild(s); |
| 14 | + k = t.Promise; |
| 15 | + t.Talk = { |
| 16 | + v: 3, |
| 17 | + ready: { |
| 18 | + then: function (f) { |
| 19 | + if (k) |
| 20 | + return new k(function (r, e) { |
| 21 | + l.push([f, r, e]); |
| 22 | + }); |
| 23 | + l.push([f]); |
| 24 | + }, |
| 25 | + catch: function () { |
| 26 | + return k && new k(); |
| 27 | + }, |
| 28 | + c: l, |
| 29 | + }, |
| 30 | + }; |
| 31 | + })(window, document, []); |
| 32 | + </script> |
| 33 | + |
| 34 | + <!-- update the version number as needed --> |
| 35 | + <script defer src="/__/firebase/11.1.0/firebase-app-compat.js"></script> |
| 36 | + <!-- include only the Firebase features as you need --> |
| 37 | + <script |
| 38 | + defer |
| 39 | + src="/__/firebase/11.1.0/firebase-messaging-compat.js" |
| 40 | + ></script> |
| 41 | + |
| 42 | + <style media="screen"> |
| 43 | + body { |
| 44 | + /* background: white; */ |
| 45 | + color: rgba(0, 0, 0, 0.87); |
| 46 | + font-family: Roboto, Helvetica, Arial, sans-serif; |
| 47 | + margin: 0; |
| 48 | + padding: 0; |
| 49 | + } |
| 50 | + .load { |
| 51 | + color: #525252; |
| 52 | + text-align: center; |
| 53 | + font-size: 13px; |
| 54 | + } |
| 55 | + #requestToken { |
| 56 | + margin: 0px 0px 12px; |
| 57 | + padding: 8px 16px; |
| 58 | + border-radius: 4px; |
| 59 | + background: #007df9; |
| 60 | + color: white; |
| 61 | + border: none; |
| 62 | + cursor: pointer; |
| 63 | + } |
| 64 | + @media (max-width: 600px) { |
| 65 | + body { |
| 66 | + margin-top: 0; |
| 67 | + background: white; |
| 68 | + box-shadow: none; |
| 69 | + } |
| 70 | + } |
| 71 | + </style> |
| 72 | + |
| 73 | + <script> |
| 74 | + Talk.ready.then(function () { |
| 75 | + var me = new Talk.User({ |
| 76 | + id: "sample_user_sebastian", |
| 77 | + name: "Sebastian", |
| 78 | + role: "default", |
| 79 | + }); |
| 80 | + var other = new Talk.User({ |
| 81 | + id: "sample_user_alice", |
| 82 | + name: "Alice", |
| 83 | + role: "default", |
| 84 | + }); |
| 85 | + |
| 86 | + window.talkSession = new Talk.Session({ |
| 87 | + appId: "<APP_ID>", // update this with your TalkJS app ID |
| 88 | + me: me, |
| 89 | + }); |
| 90 | + |
| 91 | + var conversation = talkSession.getOrCreateConversation( |
| 92 | + "sample_conversation" |
| 93 | + ); |
| 94 | + |
| 95 | + conversation.setParticipant(me); |
| 96 | + conversation.setParticipant(other); |
| 97 | + |
| 98 | + var chatbox = talkSession.createChatbox(); |
| 99 | + |
| 100 | + chatbox.select(conversation); |
| 101 | + chatbox.mount(document.getElementById("talkjs-container")); |
| 102 | + }); |
| 103 | + </script> |
| 104 | + </head> |
| 105 | + <body> |
| 106 | + <p class="load" id="load">Firebase SDK loading…</p> |
| 107 | + <p class="load" id="token"></p> |
| 108 | + |
| 109 | + <div style="display: flex; justify-content: center"> |
| 110 | + <button id="requestToken">Request token</button> |
| 111 | + </div> |
| 112 | + |
| 113 | + <div id="talkjs-container" style="height: 700px"></div> |
| 114 | + |
| 115 | + <script> |
| 116 | + document.addEventListener("DOMContentLoaded", function () { |
| 117 | + const loadEl = document.querySelector("#load"); |
| 118 | + // // 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥 |
| 119 | + // // The Firebase SDK is initialized and available here! |
| 120 | + // |
| 121 | + // firebase.auth().onAuthStateChanged(user => { }); |
| 122 | + // firebase.database().ref('/path/to/ref').on('value', snapshot => { }); |
| 123 | + // firebase.firestore().doc('/foo/bar').get().then(() => { }); |
| 124 | + // firebase.functions().httpsCallable('yourFunction')().then(() => { }); |
| 125 | + // firebase.messaging().requestPermission().then(() => { }); |
| 126 | + // firebase.storage().ref('/path/to/ref').getDownloadURL().then(() => { }); |
| 127 | + // firebase.analytics(); // call to activate |
| 128 | + // firebase.analytics().logEvent('tutorial_completed'); |
| 129 | + // firebase.performance(); // call to activate |
| 130 | + // |
| 131 | + // // 🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥🔥 |
| 132 | + |
| 133 | + try { |
| 134 | + // Update this with the firebaseConfig object from your Firebase app |
| 135 | + let app = firebase.initializeApp({ |
| 136 | + apiKey: "", |
| 137 | + authDomain: "", |
| 138 | + projectId: "", |
| 139 | + storageBucket: "", |
| 140 | + messagingSenderId: "", |
| 141 | + appId: "", |
| 142 | + }); |
| 143 | + let features = [ |
| 144 | + "auth", |
| 145 | + "database", |
| 146 | + "firestore", |
| 147 | + "functions", |
| 148 | + "messaging", |
| 149 | + "storage", |
| 150 | + "analytics", |
| 151 | + "remoteConfig", |
| 152 | + "performance", |
| 153 | + ].filter((feature) => typeof app[feature] === "function"); |
| 154 | + loadEl.textContent = `Firebase SDK loaded with ${features.join( |
| 155 | + ", " |
| 156 | + )}`; |
| 157 | + } catch (e) { |
| 158 | + console.error(e); |
| 159 | + loadEl.textContent = |
| 160 | + "Error loading the Firebase SDK, check the console."; |
| 161 | + } |
| 162 | + |
| 163 | + const messaging = firebase.messaging(firebase.app()); |
| 164 | + |
| 165 | + document |
| 166 | + .getElementById("requestToken") |
| 167 | + .addEventListener("click", () => { |
| 168 | + messaging |
| 169 | + .getToken({ |
| 170 | + vapidKey: "<FIREBASE_PUBLIC_KEY>", // update this with your Firebase project public key |
| 171 | + }) |
| 172 | + .then((token) => { |
| 173 | + window.talkSession.setPushRegistration({ |
| 174 | + provider: "fcm", |
| 175 | + pushRegistrationId: token, |
| 176 | + }); |
| 177 | + |
| 178 | + document.getElementById( |
| 179 | + "token" |
| 180 | + ).textContent = `Got token: ${token.substring(0, 16)}...`; |
| 181 | + }); |
| 182 | + }); |
| 183 | + }); |
| 184 | + </script> |
| 185 | + </body> |
| 186 | +</html> |
0 commit comments