()
| 169 | |
| 170 | // Saves the messaging device token to Cloud Firestore. |
| 171 | async function saveMessagingDeviceToken() { |
| 172 | try { |
| 173 | const currentToken = await getToken(getMessaging()); |
| 174 | if (currentToken) { |
| 175 | console.log('Got FCM device token:', currentToken); |
| 176 | // Saving the Device Token to Cloud Firestore. |
| 177 | const tokenRef = doc(getFirestore(), 'fcmTokens', currentToken); |
| 178 | await setDoc(tokenRef, { uid: getAuth().currentUser.uid }); |
| 179 | |
| 180 | // This will fire when a message is received while the app is in the foreground. |
| 181 | // When the app is in the background, firebase-messaging-sw.js will receive the message instead. |
| 182 | onMessage(getMessaging(), (message) => { |
| 183 | console.log( |
| 184 | 'New foreground notification from Firebase Messaging!', |
| 185 | message.notification |
| 186 | ); |
| 187 | }); |
| 188 | } else { |
| 189 | // Need to request permissions to show notifications. |
| 190 | requestNotificationsPermissions(); |
| 191 | } |
| 192 | } catch(error) { |
| 193 | console.error('Unable to get messaging token.', error); |
| 194 | }; |
| 195 | } |
| 196 | |
| 197 | // Requests permissions to show notifications. |
| 198 | async function requestNotificationsPermissions() { |
no test coverage detected