-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
127 lines (123 loc) · 3.73 KB
/
index.js
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
/**
* @format
*/
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import {CometChat} from '@cometchat/chat-sdk-react-native';
import messaging from '@react-native-firebase/messaging';
import {TokenRegisterHandler} from './utils/tokenRegisterHandler';
import {NotificationHandler} from './utils/notificationHandler';
import {navigate} from './src/StackNavigator';
import {AndroidStyle} from '@notifee/react-native';
import {SCREENS_CONSTANTS} from './src/CONSTS';
import {CometChatUIKit} from '@cometchat/chat-uikit-react-native';
import {COMETCHAT_CONSTANTS} from './src/CONSTS';
new TokenRegisterHandler();
new NotificationHandler();
let messageIds = [];
CometChatUIKit.init({
appId: COMETCHAT_CONSTANTS.APP_ID,
authKey: COMETCHAT_CONSTANTS.AUTH_KEY,
region: COMETCHAT_CONSTANTS.REGION,
})
.then(() => {
if (CometChat.setSource) {
CometChat.setSource('ui-kit', Platform.OS, 'react-native');
}
})
.catch(() => {
return null;
});
messaging().onMessage(async message => {
console.log('Message handled in the forground!', message);
handleMessages(message);
});
messaging().setBackgroundMessageHandler(async remoteMessage => {
console.log('Message handled in the background!', remoteMessage);
handleMessages(remoteMessage, true);
});
handleMessages = (firebaseMessage, inbackground = false) => {
try {
let msg = firebaseMessage.data
if (((msg.type == 'chat' && inbackground) || messageIds.filter(item => item === msg.tag).length)) {
NotificationHandler.displayNotification({
id: msg.tag,
title: msg.title,
body: msg.body,
data: {
conversationId: msg.conversationId,
senderUid: msg.sender,
receiverType: msg.receiverType,
guid: msg.receiver,
receiver: msg.receiver
},
android: {largeIcon: msg.senderAvatar},
});
CometChat.markAsDelivered(msg);
if(messageIds.length >= 10) {
messageIds.splice(0, 1)
messageIds.push(msg.tag);
} else {
messageIds.push(msg.tag);
}
}
if (msg.type == 'call') {
switch (msg.callAction) {
case 'initiated':
NotificationHandler.msg = msg;
NotificationHandler.displayCallAndroid();
break;
case 'ended':
CometChat.clearActiveCall();
NotificationHandler.endCall(NotificationHandler.callerId);
break;
case 'unanswered':
CometChat.clearActiveCall();
NotificationHandler.removeCallDialerWithUUID(
NotificationHandler.callerId,
);
break;
case 'busy':
CometChat.clearActiveCall();
NotificationHandler.removeCallDialerWithUUID(
NotificationHandler.callerId,
);
break;
case 'ongoing':
NotificationHandler.displayNotification({
title: msg?.receiverName || '',
body: 'ongoing call',
});
navigate({
index: 0,
routes: [
{
name: SCREENS_CONSTANTS.CALL,
params: {call: msg, needReset: true},
},
],
});
break;
case 'rejected':
CometChat.clearActiveCall();
NotificationHandler.removeCallDialerWithUUID(
NotificationHandler.callerId,
);
break;
case 'cancelled':
CometChat.clearActiveCall();
NotificationHandler.removeCallDialerWithUUID(
NotificationHandler.callerId,
);
break;
default:
break;
}
return;
}
} catch (e) {
console.log(e);
}
};
AppRegistry.registerComponent(appName, () => App);