-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
133 lines (109 loc) · 3.97 KB
/
Copy pathApp.js
File metadata and controls
133 lines (109 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import React, { useEffect } from 'react';
import { StatusBar, Linking, Platform } from 'react-native';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { NavigationContainer } from '@react-navigation/native';
import FlashMessage from 'react-native-flash-message';
import { Provider, useDispatch, useSelector } from 'react-redux';
import StackNavigator from './src/navigation/StackNavigator';
import { store } from './src/redux/store';
import { supabase } from './src/integrations/supabase/client';
import messaging from '@react-native-firebase/messaging';
import notifee, { EventType } from '@notifee/react-native';
import RingtoneService from './src/notifications/RingtoneService';
import {
setupNotificationChannel,
showIncomingWorkNotification,
} from './src/notifications/notificationService';
import IncomingWorkOverlay from './src/components/IncomingWorkOverlay';
import { clearIncomingWork, showIncomingWork } from './src/redux/workSlice';
function AppContainer() {
const dispatch = useDispatch();
// Redux state for custom notification UI
const incomingWork = useSelector(
state => state.work?.incomingWork
);
// Track current logged in worker for real-time listener
const workerId = useSelector(state => state.auth?.user?.id);
/* NOTIFICATION INIT */
useEffect(() => {
const initNotifications = async () => {
if (Platform.OS === 'android') {
await notifee.requestPermission();
}
// FCM Token (future backend)
const token = await messaging().getToken();
console.log('FCM TOKEN:', token);
await setupNotificationChannel();
// Foreground FCM listener (future use)
messaging().onMessage(async remoteMessage => {
if (remoteMessage.data?.type === 'NEW_WORK') {
await showIncomingWorkNotification(remoteMessage.data);
RingtoneService.startRingtone();
}
});
// Notification action handler
notifee.onForegroundEvent(async ({ type, detail }) => {
if (type === EventType.ACTION_PRESS) {
RingtoneService.stopRingtone?.();
const notificationId = detail.notification?.id;
if (notificationId) {
await notifee.cancelNotification(notificationId);
}
}
});
};
initNotifications();
}, []);
/* SUPABASE REALTIME NOTIFICATIONS */
useEffect(() => {
if (!workerId) return;
console.log(`Setting up Supabase real-time listener for worker ${workerId}...`);
const channel = supabase.channel('worker_notifications');
const subscription = channel
.on('broadcast', { event: `new_task_worker_${workerId}` }, (payload) => {
const jobDetails = payload.payload;
console.log('Received new task broadcast:', jobDetails);
// Show IncomingWorkOverlay and play ringtone
dispatch(showIncomingWork(jobDetails));
RingtoneService.startRingtone();
})
.subscribe((status) => {
console.log(`Supabase Realtime status for worker ${workerId}:`, status);
});
return () => {
console.log(`Unsubscribing from worker_notifications for worker ${workerId}`);
supabase.removeChannel(channel);
};
}, [workerId, dispatch]);
/* UI*/
return (
<SafeAreaProvider>
<StatusBar barStyle="light-content" backgroundColor="#4A72FF" />
<NavigationContainer>
<StackNavigator />
</NavigationContainer>
{/* CUSTOM NOTIFICATION */}
{incomingWork && (
<IncomingWorkOverlay
data={incomingWork}
onAccept={() => {
RingtoneService.stopRingtone?.();
dispatch(clearIncomingWork());
}}
onReject={() => {
RingtoneService.stopRingtone?.();
dispatch(clearIncomingWork());
}}
/>
)}
<FlashMessage position="top" statusBarHeight={15} />
</SafeAreaProvider>
);
}
export default function App() {
return (
<Provider store={store}>
<AppContainer />
</Provider>
);
}