|
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'; |
7 | 3 | //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'; |
11 | 7 |
|
12 | 8 | let globalFcmToken: string | null = null; |
13 | 9 | export const usePushNotifications = () => { |
14 | | - const [fcmToken, setFcmToken] = useState<string | null>(globalFcmToken); |
| 10 | + const [fcmToken, setFcmToken] = useState<string | null>(globalFcmToken); |
15 | 11 |
|
16 | | - useEffect(() => { |
17 | | - const initializeNotifications = async () => { |
18 | | - getFirebaseApp(); |
| 12 | + useEffect(() => { |
| 13 | + const initializeNotifications = async () => { |
| 14 | + getFirebaseApp(); |
19 | 15 |
|
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 | + }); |
31 | 24 |
|
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); |
36 | 29 |
|
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); |
41 | 34 |
|
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 | + }; |
51 | 44 |
|
52 | | - initializeNotifications(); |
53 | | - handleBackgroundNotifications(); |
54 | | - const unsubscribe = handleForegroundNotification(); |
| 45 | + initializeNotifications(); |
| 46 | + handleBackgroundNotifications(); |
| 47 | + const unsubscribe = handleForegroundNotification(); |
55 | 48 |
|
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 | + }); |
66 | 58 |
|
67 | | - return () => { |
68 | | - unsubscribe(); |
69 | | - }; |
70 | | - }, []); |
| 59 | + return () => { |
| 60 | + unsubscribe(); |
| 61 | + }; |
| 62 | + }, []); |
71 | 63 |
|
72 | | - return { fcmToken }; |
| 64 | + return { fcmToken }; |
73 | 65 | }; |
74 | 66 |
|
75 | 67 | export const getFcmToken = () => globalFcmToken; |
76 | 68 |
|
77 | 69 | 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 | + }; |
85 | 77 |
|
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 | + }); |
91 | 83 |
|
92 | | - const subscription = Notifications.addNotificationResponseReceivedListener( |
93 | | - (response) => { |
94 | | - redirectToUrl(response.notification); |
95 | | - } |
96 | | - ); |
| 84 | + const subscription = Notifications.addNotificationResponseReceivedListener((response) => { |
| 85 | + redirectToUrl(response.notification); |
| 86 | + }); |
97 | 87 |
|
98 | | - return () => { |
99 | | - subscription.remove(); |
100 | | - }; |
101 | | - }, []); |
| 88 | + return () => { |
| 89 | + subscription.remove(); |
| 90 | + }; |
| 91 | + }, []); |
102 | 92 | }; |
0 commit comments