Skip to content

Commit a1a03f2

Browse files
Refactoring push notification hooks.
1 parent 4546049 commit a1a03f2

1 file changed

Lines changed: 70 additions & 80 deletions

File tree

hooks/usePushNotifications.ts

Lines changed: 70 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,92 @@
1-
import { useEffect, useState } from "react";
2-
import {
3-
handleForegroundNotification,
4-
handleBackgroundNotifications,
5-
registerForPushNotificationsAsync,
6-
} from "../utils/notifications.utils";
1+
import { useEffect, useState } from 'react';
2+
import { handleForegroundNotification, handleBackgroundNotifications, registerForPushNotificationsAsync } from '../utils/notifications.utils';
73
//import messaging from "@react-native-firebase/messaging";
8-
import * as Notifications from "expo-notifications";
9-
import { router } from "expo-router";
10-
import { getFirebaseApp } from "@/utils/firebase.utils";
4+
import * as Notifications from 'expo-notifications';
5+
import { router } from 'expo-router';
6+
import { getFirebaseApp } from '@/utils/firebase.utils';
117

128
let globalFcmToken: string | null = null;
139
export const usePushNotifications = () => {
14-
const [fcmToken, setFcmToken] = useState<string | null>(globalFcmToken);
10+
const [fcmToken, setFcmToken] = useState<string | null>(globalFcmToken);
1511

16-
useEffect(() => {
17-
const initializeNotifications = async () => {
18-
getFirebaseApp();
12+
useEffect(() => {
13+
const initializeNotifications = async () => {
14+
getFirebaseApp();
1915

20-
// Register for push notifications and get the push token
21-
registerForPushNotificationsAsync().then(async (token) => {
22-
if (token) {
23-
console.log(
24-
"Finished registering for Notifications - FCM Token:",
25-
token
26-
);
27-
setFcmToken(token); // Store the push token
28-
globalFcmToken = token; // Update the global token
29-
}
30-
});
16+
// Register for push notifications and get the push token
17+
registerForPushNotificationsAsync().then(async (token) => {
18+
if (token) {
19+
console.log('Finished registering for Notifications - FCM Token:', token);
20+
setFcmToken(token); // Store the push token
21+
globalFcmToken = token; // Update the global token
22+
}
23+
});
3124

32-
// const hasPermission = await requestPermission();
33-
// if (hasPermission) {
34-
// const token = await messaging().getToken();
35-
// console.log("FCM Token:", token);
25+
// const hasPermission = await requestPermission();
26+
// if (hasPermission) {
27+
// const token = await messaging().getToken();
28+
// console.log("FCM Token:", token);
3629

37-
// // Check if the token is already saved globally
38-
// if (token && token !== globalFcmToken) {
39-
// globalFcmToken = token; // Store in a global variable
40-
// setFcmToken(token);
30+
// // Check if the token is already saved globally
31+
// if (token && token !== globalFcmToken) {
32+
// globalFcmToken = token; // Store in a global variable
33+
// setFcmToken(token);
4134

42-
// // Save the token to the server
43-
// try {
44-
// await savePushToken(token);
45-
// } catch (error) {
46-
// console.error("Error saving push token:", error);
47-
// }
48-
// }
49-
// }
50-
};
35+
// // Save the token to the server
36+
// try {
37+
// await savePushToken(token);
38+
// } catch (error) {
39+
// console.error("Error saving push token:", error);
40+
// }
41+
// }
42+
// }
43+
};
5144

52-
initializeNotifications();
53-
handleBackgroundNotifications();
54-
const unsubscribe = handleForegroundNotification();
45+
initializeNotifications();
46+
handleBackgroundNotifications();
47+
const unsubscribe = handleForegroundNotification();
5548

56-
// Set a global notification handler
57-
Notifications.setNotificationHandler({
58-
handleNotification: async () => ({
59-
shouldShowAlert: true,
60-
shouldPlaySound: true,
61-
shouldSetBadge: true,
62-
shouldShowBanner: true,
63-
shouldShowList: true,
64-
}),
65-
});
49+
// Set a global notification handler
50+
Notifications.setNotificationHandler({
51+
handleNotification: async () => ({
52+
shouldPlaySound: true,
53+
shouldSetBadge: true,
54+
shouldShowBanner: true,
55+
shouldShowList: true,
56+
}),
57+
});
6658

67-
return () => {
68-
unsubscribe();
69-
};
70-
}, []);
59+
return () => {
60+
unsubscribe();
61+
};
62+
}, []);
7163

72-
return { fcmToken };
64+
return { fcmToken };
7365
};
7466

7567
export const getFcmToken = () => globalFcmToken;
7668

7769
export const useNotificationObserver = () => {
78-
useEffect(() => {
79-
const redirectToUrl = (notification: Notifications.Notification) => {
80-
const url = notification.request.content.data?.url;
81-
if (typeof url === "string") {
82-
router.push(url as any);
83-
}
84-
};
70+
useEffect(() => {
71+
const redirectToUrl = (notification: Notifications.Notification) => {
72+
const url = notification.request.content.data?.url;
73+
if (typeof url === 'string') {
74+
router.push(url as any);
75+
}
76+
};
8577

86-
Notifications.getLastNotificationResponseAsync().then((response) => {
87-
if (response?.notification) {
88-
redirectToUrl(response.notification);
89-
}
90-
});
78+
Notifications.getLastNotificationResponseAsync().then((response) => {
79+
if (response?.notification) {
80+
redirectToUrl(response.notification);
81+
}
82+
});
9183

92-
const subscription = Notifications.addNotificationResponseReceivedListener(
93-
(response) => {
94-
redirectToUrl(response.notification);
95-
}
96-
);
84+
const subscription = Notifications.addNotificationResponseReceivedListener((response) => {
85+
redirectToUrl(response.notification);
86+
});
9787

98-
return () => {
99-
subscription.remove();
100-
};
101-
}, []);
88+
return () => {
89+
subscription.remove();
90+
};
91+
}, []);
10292
};

0 commit comments

Comments
 (0)