-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathindex.js
More file actions
180 lines (170 loc) · 6.2 KB
/
Copy pathindex.js
File metadata and controls
180 lines (170 loc) · 6.2 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import {AppRegistry, Platform} from 'react-native';
import messaging from '@react-native-firebase/messaging';
import App from './App';
import {name as appName} from './app.json';
import {voipHandler} from './src/utils/VoipNotificationHandler';
import {CometChat} from '@cometchat/chat-sdk-react-native';
import {navigationRef} from './src/navigation/NavigationService';
import {displayLocalNotification} from './src/utils/helper';
import notifee, {EventType} from '@notifee/react-native';
import {StackActions} from '@react-navigation/native';
import AppErrorBoundary from './AppErrorBoundary';
import {ActiveChatProvider} from './src/utils/ActiveChatContext';
if (global?.ErrorUtils) {
const defaultHandler = global.ErrorUtils.getGlobalHandler();
function globalErrorHandler(error, isFatal) {
console.log(
'[GlobalErrorHandler]:',
isFatal ? 'Fatal:' : 'Non-Fatal:',
error,
);
defaultHandler?.(error, isFatal);
}
global.ErrorUtils.setGlobalHandler(globalErrorHandler);
}
if (typeof process === 'object' && process.on) {
process.on('unhandledRejection', (reason, promise) => {
console.log('[Unhandled Promise Rejection]:', reason);
});
}
const Root = () => (
<AppErrorBoundary>
<ActiveChatProvider>
<App />
</ActiveChatProvider>
</AppErrorBoundary>
);
// Run Notifee background event handler only on Android
if (Platform.OS === 'android') {
notifee.onBackgroundEvent(async ({type, detail}) => {
try {
if (type === EventType.PRESS) {
const {notification} = detail;
if (notification?.id) {
await notifee.cancelNotification(notification.id);
}
const data = detail?.notification?.data || {};
if (data.receiverType === 'group') {
const extractedId =
typeof data.conversationId === 'string'
? data.conversationId.split('_').slice(1).join('_')
: '';
CometChat.getGroup(extractedId).then(
group => {
// Mark conversation as read when opening from push notification
CometChat.markConversationAsRead(extractedId, CometChat.RECEIVER_TYPE.GROUP).catch(
e => console.log('Error marking group conversation as read:', e),
);
navigationRef.current?.dispatch(
StackActions.push('Messages', {
group,
parentMessageId: data.parentId,
}),
);
},
error => console.log('Error fetching group details:', error),
);
} else if (data.receiverType === 'user') {
CometChat.getUser(data.sender).then(
ccUser => {
// Mark conversation as read when opening from push notification
CometChat.markConversationAsRead(data.sender, CometChat.RECEIVER_TYPE.USER).catch(
e => console.log('Error marking user conversation as read:', e),
);
navigationRef.current?.dispatch(
StackActions.push('Messages', {
user: ccUser,
parentMessageId: data.parentId,
}),
);
},
error => console.log('Error fetching user details:', error),
);
}
}
} catch (error) {
console.log('Error handling notifee background event:', error);
}
});
}
// This runs for background/killed states on Android.
if (Platform.OS === 'android') {
messaging().setBackgroundMessageHandler(async remoteMessage => {
try {
const data = remoteMessage.data || {};
if (data.type === 'call') {
await voipHandler.initialize();
switch (data.callAction) {
case 'initiated':
voipHandler.msg = data;
await voipHandler.displayCallAndroid();
break;
case 'ended':
CometChat.clearActiveCall();
await voipHandler.endCall({callUUID: voipHandler.callerId});
break;
case 'unanswered':
CometChat.clearActiveCall();
if (voipHandler?.callerId) {
voipHandler.removeCallDialerWithUUID(voipHandler.callerId);
} else {
console.warn('Caller ID is missing. Cannot remove call dialer.');
}
break;
case 'busy':
CometChat.clearActiveCall();
if (voipHandler?.callerId) {
voipHandler.removeCallDialerWithUUID(voipHandler.callerId);
} else {
console.warn('Caller ID is missing. Cannot remove call dialer.');
}
break;
case 'ongoing':
voipHandler.displayNotification({
title: data?.receiverName || '',
body: 'ongoing call',
});
break;
case 'rejected':
CometChat.clearActiveCall();
if (voipHandler?.callerId) {
voipHandler.removeCallDialerWithUUID(voipHandler.callerId);
} else {
console.warn('Caller ID is missing. Cannot remove call dialer.');
}
break;
case 'cancelled':
CometChat.clearActiveCall();
if (voipHandler?.callerId) {
voipHandler.removeCallDialerWithUUID(voipHandler.callerId);
} else {
console.warn('Caller ID is missing. Cannot remove call dialer.');
}
break;
default:
break;
}
return;
} else {
// Handle badge count from push notification
const unreadCount = data?.unreadMessageCount;
if (unreadCount !== undefined && unreadCount !== null) {
const count = parseInt(unreadCount, 10);
if (!isNaN(count) && count >= 0) {
try {
await notifee.setBadgeCount(count);
} catch (error) {
console.error('Error setting badge:', error);
}
}
} else {
console.log('No unreadMessageCount in payload - check dashboard settings');
}
await displayLocalNotification(remoteMessage);
}
} catch (error) {
console.error('Error in background message handler:', error);
}
});
}
AppRegistry.registerComponent(appName, () => Root);