Skip to content

Commit b9b2bc3

Browse files
Dean SoferDean Sofer
authored andcommitted
Updating fcm token upon login
1 parent 369eb01 commit b9b2bc3

File tree

4 files changed

+22
-65
lines changed

4 files changed

+22
-65
lines changed

src/Dialogues/Friends.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { UserData, Match, SnapshotOrNullType } from '../Types';
1010
import Avatar from '../Avatar';
1111
import './Friends.css'
1212
import ToggleFullscreen from './ToggleFullscreen';
13-
import { saveMessagingDeviceToken } from '../firebase-messaging-setup';
13+
import { saveFcmToken } from '../firebase.config';
1414

1515
type Users = { [key: string]: UserData }
1616

@@ -28,24 +28,26 @@ export default function Friends({ user, load, reset }: FriendsProps) {
2828
const [isExpanded, setIsExpanded] = useState(false);
2929
const [matches, setMatches] = useState<firebase.database.DataSnapshot>([]);
3030
const [searchResults, setSearchResults] = useState<firebase.database.DataSnapshot>([]);
31-
const [hasAttemptedNotificationPermission, setHasAttemptedNotificationPermission] = useState(false);
31+
const [requestedNotifications, setRequestedNotifications] = useState(false);
3232

3333
// Request notification permission when Friends component mounts
3434
useEffect(() => {
3535
const requestPermission = async () => {
36-
if (user && Notification.permission === 'default' && !hasAttemptedNotificationPermission) {
36+
if (user && Notification.permission === 'default' && !requestedNotifications) {
3737
console.log("Friends modal opened, attempting notification permission request...");
3838
try {
39-
await saveMessagingDeviceToken();
39+
const permission = await Notification.requestPermission();
40+
if (permission === 'granted')
41+
saveFcmToken();
4042
} catch (error) {
4143
console.error("Error requesting notification permission:", error);
4244
} finally {
43-
setHasAttemptedNotificationPermission(true);
45+
setRequestedNotifications(true);
4446
}
4547
}
4648
};
4749
requestPermission();
48-
}, [user, hasAttemptedNotificationPermission]);
50+
}, [user, requestedNotifications]);
4951

5052
// Synchronize Matches
5153
useEffect(() => {

src/firebase-messaging-setup.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/firebase.config.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,14 @@ export const config = {
1515
measurementId: "G-NKGPNTLDF1"
1616
};
1717

18-
export default firebase.initializeApp(config);
18+
const vapidKey = 'BM1H9qfv1e_XcIB31ZeLCn8IpGOdMIwMShRej6wld8QAMkV4YqJ-eMQa1rSnwhkmVmAFw3tvUdlP2JzZmgTq4Fk';
19+
20+
export default firebase.initializeApp(config);
21+
22+
export async function saveFcmToken() {
23+
const userId = firebase.auth().currentUser?.uid;
24+
if (Notification.permission === 'granted' && userId)
25+
await firebase.database().ref(`/users/${userId}/fcmToken`).set(
26+
await firebase.messaging().getToken({ vapidKey })
27+
)
28+
}

src/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import './index.css'
1313
import './Board/Board.css';
1414
import './Board/Toolbar.css'
1515
import { calculate, newGame, nextMoves, populated, rollDie, vibrate } from './Utils';
16-
import firebase from "./firebase.config";
16+
import firebase, { saveFcmToken } from "./firebase.config";
1717
import { playCheckerSound } from './Utils';
1818
import type firebaseType from 'firebase/compat/app';
1919

@@ -113,6 +113,7 @@ export function App() {
113113
};
114114
console.log('Creating user', data);
115115
userRef.set(data);
116+
saveFcmToken();
116117
}
117118
userRef.on('value', user => {
118119
setUser(user);
@@ -129,7 +130,7 @@ export function App() {
129130
const moves = useMemo(() => {
130131
if (game.turn && (game.turn !== user?.val().uid || game.status !== Status.Moving))
131132
return new Set();
132-
return nextMoves(game, usedDice, selected)
133+
return nextMoves(game, usedDice, selected!)
133134
}, [selected, game, usedDice])
134135

135136
// Subscribe to match

0 commit comments

Comments
 (0)