Skip to content

Commit 101cf60

Browse files
Dean SoferDean Sofer
authored andcommitted
Trying to improve notification request behavior
1 parent 89df0f8 commit 101cf60

File tree

3 files changed

+13
-24
lines changed

3 files changed

+13
-24
lines changed

src/Dialogues/Friends.tsx

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,6 @@ 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 [requestedNotifications, setRequestedNotifications] = useState(false);
32-
33-
// Request notification permission when Friends component mounts
34-
useEffect(() => {
35-
const requestPermission = async () => {
36-
if (user && Notification.permission === 'default' && !requestedNotifications) {
37-
console.log("Friends modal opened, attempting notification permission request...");
38-
try {
39-
const permission = await Notification.requestPermission();
40-
if (permission === 'granted')
41-
saveFcmToken();
42-
} catch (error) {
43-
console.error("Error requesting notification permission:", error);
44-
} finally {
45-
setRequestedNotifications(true);
46-
}
47-
}
48-
};
49-
requestPermission();
50-
}, [user, requestedNotifications]);
5131

5232
// Synchronize Matches
5333
useEffect(() => {
@@ -171,6 +151,12 @@ export default function Friends({ user, load, reset }: FriendsProps) {
171151
Edit Profile
172152
</a>
173153
</li>
154+
{Notification.permission === 'default' ? <li>
155+
<a onPointerUp={() => saveFcmToken(true)} href="#">
156+
<span className="material-icons notranslate">notifications</span>
157+
Notifications
158+
</a>
159+
</li> : null}
174160
<li>
175161
<a onPointerUp={handleReset} href="#">
176162
<span className="material-icons notranslate">restart_alt</span>

src/firebase.config.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ const vapidKey = 'BM1H9qfv1e_XcIB31ZeLCn8IpGOdMIwMShRej6wld8QAMkV4YqJ-eMQa1rSnwh
1919

2020
export default firebase.initializeApp(config);
2121

22-
export async function saveFcmToken() {
22+
export async function saveFcmToken(requestPermission = false) {
2323
const userId = firebase.auth().currentUser?.uid;
24-
if (Notification.permission === 'granted' && userId)
24+
if (!userId) return;
25+
if (Notification.permission === 'default' && requestPermission)
26+
await Notification.requestPermission();
27+
if (Notification.permission === 'granted')
2528
await firebase.database().ref(`/users/${userId}/fcmToken`).set(
2629
await firebase.messaging().getToken({ vapidKey })
2730
)

src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ export function App() {
139139
};
140140
console.log('Creating user', data);
141141
userRef.set(data);
142-
saveFcmToken(); // Ensure FCM token is saved for new users
142+
saveFcmToken(true);
143143
} else if (!snapshot.val().fcmToken) {
144-
saveFcmToken(); // Ensure FCM token is saved if missing for existing users
144+
saveFcmToken();
145145
}
146146

147147
// Subscribe to user data changes

0 commit comments

Comments
 (0)