Skip to content

Commit

Permalink
DMAPP-135: Integrate chat notification
Browse files Browse the repository at this point in the history
- Handled rare case for missing notification data: navigates to the chat list instead of a specific chat.
- Minor changes and fixes for improved reliability.
  • Loading branch information
meKushdeepSingh committed Dec 24, 2024
1 parent 93826a7 commit e38d8ae
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
69 changes: 45 additions & 24 deletions src/contexts/NotificationContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import messaging, {
} from '@react-native-firebase/messaging';
import React, {createContext, useEffect, useRef, useState} from 'react';
import {Platform} from 'react-native';
import {useSelector} from 'react-redux';
import GLOBALS from 'src/Globals';
import {NotificationHelper} from 'src/helpers/NotificationHelper';
import {usePushApiMode} from 'src/hooks/pushapi/usePushApiMode';
Expand All @@ -17,6 +18,7 @@ import {
navigate,
replaceRoute,
} from 'src/navigation/RootNavigation';
import {selectCurrentUser, selectUsers} from 'src/redux/authSlice';

import {usePushApi} from './PushApiContext';

Expand Down Expand Up @@ -70,6 +72,12 @@ export const NotificationContextProvider = ({
const {isChatEnabled} = usePushApiMode();
const {showUnlockProfileModal, userPushSDKInstance} = usePushApi();

const users = useSelector(selectUsers);
const currentUser = useSelector(selectCurrentUser);

const wallet = users[currentUser]?.wallet;
const pkey = users[currentUser]?.userPKey;

const isChatEnabledRef = useRef<boolean>(isChatEnabled);

const [tempNotificationData, setTempNotificationData] =
Expand Down Expand Up @@ -215,8 +223,8 @@ export const NotificationContextProvider = ({
const recentChatNotificationsIDs = recentNotifications
.filter(
item =>
JSON.parse(item.notification.data?.details as string)?.info
?.chatId === chatId,
JSON.parse((item.notification.data?.details as string) ?? '{}')
?.info?.chatId === chatId,
)
.map(item => `${item.id}`);
await notifee.cancelDisplayedNotifications(recentChatNotificationsIDs);
Expand Down Expand Up @@ -287,36 +295,49 @@ export const NotificationContextProvider = ({
} else {
// If Home(Notification) tab is inactive then first
// navigate to Home tab then update data
navigate(GLOBALS.SCREENS.NOTIF_TABS);
navigate(GLOBALS.SCREENS.NOTIF_TABS, {
wallet,
});
setChannelNotificationOpened(true);
}
} else if (data?.type === NOTIFICATION_TYPES.CHAT) {
// Handle Chat notification banner press
const resolvedCanAccessChat = canAccessChat ?? isChatEnabledRef.current; // Use ref for default
if (resolvedCanAccessChat) {
try {
// Get navigation params for SINGLE CHAT Screen
const isGroupConversation =
parsedDetails?.subType === NOTIFICATION_SUB_TYPES.GROUP_CHAT;
const singleChatParams =
await NotificationHelper.getChatNavigationParams({
chatId: parsedDetails?.info?.chatId,
userPushSDKInstance: userPushSDKInstance,
isGroupConversation,
wallets: parsedDetails?.info?.wallets,
profilePicture: parsedDetails?.info?.profilePicture,
threadhash: parsedDetails?.info?.threadhash,
});
if (singleChatParams) {
if (getCurrentRouteName() === GLOBALS.SCREENS.SINGLE_CHAT) {
// If Single chat screen is already active then update params data
replaceRoute(GLOBALS.SCREENS.SINGLE_CHAT, singleChatParams);
setTempNotificationData(null);
} else {
// Navigate to Single/Group chat screen
navigate(GLOBALS.SCREENS.SINGLE_CHAT, singleChatParams);
setTempNotificationData(null);
if (parsedDetails?.subType) {
// Get navigation params for SINGLE CHAT Screen
const isGroupConversation =
parsedDetails?.subType === NOTIFICATION_SUB_TYPES.GROUP_CHAT;
const singleChatParams =
await NotificationHelper.getChatNavigationParams({
chatId: parsedDetails?.info?.chatId,
userPushSDKInstance: userPushSDKInstance,
isGroupConversation,
wallets: parsedDetails?.info?.wallets,
profilePicture: parsedDetails?.info?.profilePicture,
threadhash: parsedDetails?.info?.threadhash,
});
if (singleChatParams) {
if (getCurrentRouteName() === GLOBALS.SCREENS.SINGLE_CHAT) {
// If Single chat screen is already active then update params data
replaceRoute(GLOBALS.SCREENS.SINGLE_CHAT, singleChatParams);
setTempNotificationData(null);
} else {
// Navigate to Single/Group chat screen
navigate(GLOBALS.SCREENS.SINGLE_CHAT, singleChatParams);
setTempNotificationData(null);
}
}
} else if (
!parsedDetails?.subType &&
getCurrentRouteName() !== GLOBALS.SCREENS.CHATS
) {
// Handled rare case for missing notification data: navigates to the chat list instead of a specific chat.
navigate(GLOBALS.SCREENS.CHATS, {
wallet,
pkey,
});
}
} catch (error) {
console.log('Notification Route ERROR', error);
Expand Down
5 changes: 3 additions & 2 deletions src/helpers/NotificationHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ export const NotificationHelper: NotificationHelperType = {
},
categoryId: 'Communications',
communicationInfo: {
conversationId: parsedDetails?.info?.chatId,
conversationId:
parsedDetails?.info?.chatId ?? remoteMessage.messageId,
sender: {
id: parsedDetails?.info?.wallets,
id: parsedDetails?.info?.wallets ?? remoteMessage.messageId,
displayName: remoteMessage.notification?.title ?? '',
},
},
Expand Down

0 comments on commit e38d8ae

Please sign in to comment.