From e53ce36a9e2f77992670316f772e76ad5242243b Mon Sep 17 00:00:00 2001 From: Yunsup Sim Date: Sun, 26 Dec 2021 02:40:50 +0900 Subject: [PATCH] Delete same notification if enter the channel. (#476) * Delete same notification if enter the channel. * Delete notification if user is in channel Delete notification if user is in channel, but not alert. * Format eslint * Rename function `camelCase` --- client/src/components/pages/Message.tsx | 47 +++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/client/src/components/pages/Message.tsx b/client/src/components/pages/Message.tsx index c8d6b097e..f0021f16c 100644 --- a/client/src/components/pages/Message.tsx +++ b/client/src/components/pages/Message.tsx @@ -1,3 +1,5 @@ +import * as Notifications from 'expo-notifications'; + import { ActivityIndicator, Alert, @@ -636,6 +638,51 @@ const MessageScreen: FC = () => { params: {channelId}, } = useRoute>(); + // Delete notification if user is foreground in channel. + useEffect(() => { + const subscription = Notifications.addNotificationReceivedListener( + (notification) => { + const parsedNotificationData = JSON.parse( + notification.request.content.data.data as string, + ); + + if (parsedNotificationData.channelId === channelId) { + Notifications.dismissNotificationAsync( + notification.request.identifier, + ); + } + }, + ); + + return () => subscription.remove(); + }, [channelId]); + + useEffect(() => { + async function deleteSameChannelNotification(): Promise { + const notifications = + await Notifications.getPresentedNotificationsAsync(); + + notifications.forEach((notificationData) => { + const {data: jsonString} = notificationData.request.content.data; + + if (typeof jsonString === 'string') { + const {channelId: notificationChannelId} = JSON.parse(jsonString); + + if ( + typeof notificationChannelId === 'string' && + notificationChannelId === channelId + ) { + Notifications.dismissNotificationAsync( + notificationData.request.identifier, + ); + } + } + }); + } + + deleteSameChannelNotification(); + }, [channelId]); + const searchArgs: MessagesQueryVariables = { first: ITEM_CNT, channelId,