|
@@ -145,35 +145,94 @@ const ChatScreen = ({ route }: { route: any }) => {
|
|
|
});
|
|
|
return;
|
|
|
}
|
|
|
- const subscription = Notifications.addNotificationReceivedListener((notification) => {
|
|
|
- let conversation_with_user = 0;
|
|
|
+
|
|
|
+ const getNotificationData = (notification: Notifications.Notification) => {
|
|
|
if (Platform.OS === 'android') {
|
|
|
const data = notification.request.content.data;
|
|
|
if (data?.params) {
|
|
|
- const parsedParams = JSON.parse(data.params) ?? {};
|
|
|
- conversation_with_user = parsedParams?.id;
|
|
|
+ try {
|
|
|
+ return JSON.parse(data.params) ?? {};
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error parsing params:', error);
|
|
|
+ return {};
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ Notifications.dismissNotificationAsync(notification.request.identifier);
|
|
|
+ return {};
|
|
|
}
|
|
|
} else {
|
|
|
const data = (notification.request.trigger as Notifications.PushNotificationTrigger)
|
|
|
?.payload;
|
|
|
if (data?.params) {
|
|
|
- const parsedParams = JSON.parse(data.params as string) ?? {};
|
|
|
- conversation_with_user = parsedParams?.id;
|
|
|
+ try {
|
|
|
+ return JSON.parse(data.params as string) ?? {};
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error parsing params:', error);
|
|
|
+ return {};
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
+ };
|
|
|
+
|
|
|
+ const clearNotificationsFromUser = async (userId: number) => {
|
|
|
+ const presentedNotifications = await Notifications.getPresentedNotificationsAsync();
|
|
|
+ presentedNotifications.forEach((notification) => {
|
|
|
+ const parsedParams = getNotificationData(notification);
|
|
|
+ const conversation_with_user = parsedParams?.id;
|
|
|
+
|
|
|
+ if (conversation_with_user === userId) {
|
|
|
+ Notifications.dismissNotificationAsync(notification.request.identifier);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
+ await clearNotificationsFromUser(chatWithUserId);
|
|
|
+
|
|
|
+ Notifications.setNotificationHandler({
|
|
|
+ handleNotification: async (notification) => {
|
|
|
+ let conversation_with_user = 0;
|
|
|
+ const parsedParams = getNotificationData(notification);
|
|
|
+ conversation_with_user = parsedParams?.id;
|
|
|
|
|
|
- if (conversation_with_user === chatWithUserId) {
|
|
|
- Notifications.dismissNotificationAsync(notification.request.identifier);
|
|
|
+ if (conversation_with_user === chatWithUserId) {
|
|
|
+ return {
|
|
|
+ shouldShowAlert: false,
|
|
|
+ shouldPlaySound: false,
|
|
|
+ shouldSetBadge: false
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ shouldShowAlert: true,
|
|
|
+ shouldPlaySound: false,
|
|
|
+ shouldSetBadge: false
|
|
|
+ };
|
|
|
}
|
|
|
});
|
|
|
|
|
|
return () => {
|
|
|
- subscription.remove();
|
|
|
+ Notifications.setNotificationHandler({
|
|
|
+ handleNotification: async () => ({
|
|
|
+ shouldShowAlert: true,
|
|
|
+ shouldPlaySound: false,
|
|
|
+ shouldSetBadge: false
|
|
|
+ })
|
|
|
+ });
|
|
|
};
|
|
|
};
|
|
|
|
|
|
useEffect(() => {
|
|
|
- dismissChatNotifications(id);
|
|
|
+ let unsubscribe: any;
|
|
|
+
|
|
|
+ const setupNotificationHandler = async () => {
|
|
|
+ unsubscribe = await dismissChatNotifications(id);
|
|
|
+ };
|
|
|
+
|
|
|
+ setupNotificationHandler();
|
|
|
+
|
|
|
+ return () => {
|
|
|
+ if (unsubscribe) unsubscribe();
|
|
|
+ };
|
|
|
}, [id]);
|
|
|
|
|
|
useEffect(() => {
|
|
@@ -269,7 +328,7 @@ const ChatScreen = ({ route }: { route: any }) => {
|
|
|
if (chatData?.messages) {
|
|
|
const mappedMessages = chatData.messages.map(mapApiMessageToGiftedMessage);
|
|
|
|
|
|
- if (unreadMessageIndex === null) {
|
|
|
+ if (unreadMessageIndex === null && Platform.OS === 'ios') {
|
|
|
const firstUnreadIndex = mappedMessages.findLastIndex(
|
|
|
(msg) => !msg.received && !msg?.deleted && msg.user._id === id
|
|
|
);
|