From b303c88ac201dc478015e82c2cb6a3cbe721adad Mon Sep 17 00:00:00 2001 From: Hailey Date: Wed, 15 May 2024 20:29:29 -0700 Subject: [PATCH] decrement badge count for chats --- src/lib/hooks/useNotificationHandler.ts | 7 +++++-- src/lib/notifications/notifications.ts | 12 ++++++++++++ src/screens/Messages/List/ChatListItem.tsx | 10 ++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/lib/hooks/useNotificationHandler.ts b/src/lib/hooks/useNotificationHandler.ts index 6f5fbd66bb..2bc004ec5d 100644 --- a/src/lib/hooks/useNotificationHandler.ts +++ b/src/lib/hooks/useNotificationHandler.ts @@ -6,6 +6,7 @@ import {useQueryClient} from '@tanstack/react-query' import {logger} from '#/logger' import {track} from 'lib/analytics/analytics' import {useAccountSwitcher} from 'lib/hooks/useAccountSwitcher' +import {decrementBadgeCount} from 'lib/notifications/notifications' import {NavigationProp} from 'lib/routes/types' import {logEvent} from 'lib/statsig/statsig' import {isAndroid} from 'platform/detection' @@ -88,6 +89,7 @@ export function useNotificationsHandler() { if (!payload) return if (payload.reason === 'chat-message') { + decrementBadgeCount(1) if (payload.recipientDid !== currentAccount?.did && !storedPayload) { storedPayload = payload closeAllActiveElements() @@ -169,10 +171,11 @@ export function useNotificationsHandler() { payload.reason === 'chat-message' && payload.recipientDid === currentAccount?.did ) { + const isCurrentConvo = payload.convoId === currentConvoId return { - shouldShowAlert: payload.convoId !== currentConvoId, + shouldShowAlert: isCurrentConvo, shouldPlaySound: false, - shouldSetBadge: false, + shouldSetBadge: isCurrentConvo, } } diff --git a/src/lib/notifications/notifications.ts b/src/lib/notifications/notifications.ts index 52f984a599..1182bfcbbe 100644 --- a/src/lib/notifications/notifications.ts +++ b/src/lib/notifications/notifications.ts @@ -1,5 +1,6 @@ import React from 'react' import * as Notifications from 'expo-notifications' +import {getBadgeCountAsync, setBadgeCountAsync} from 'expo-notifications' import {BskyAgent} from '@atproto/api' import {logger} from '#/logger' @@ -109,3 +110,14 @@ export function useRequestNotificationsPermission() { [gate], ) } + +export async function decrementBadgeCount(by = 1) { + if (!isNative) return + + const currCount = await getBadgeCountAsync() + let newCount = currCount - by + if (newCount < 0) { + newCount = 0 + } + await setBadgeCountAsync(newCount) +} diff --git a/src/screens/Messages/List/ChatListItem.tsx b/src/screens/Messages/List/ChatListItem.tsx index 0a0f8c5755..fd2d46edda 100644 --- a/src/screens/Messages/List/ChatListItem.tsx +++ b/src/screens/Messages/List/ChatListItem.tsx @@ -15,6 +15,7 @@ import {isNative} from '#/platform/detection' import {useProfileShadow} from '#/state/cache/profile-shadow' import {useModerationOpts} from '#/state/preferences/moderation-opts' import {useSession} from '#/state/session' +import {decrementBadgeCount} from 'lib/notifications/notifications' import {sanitizeDisplayName} from 'lib/strings/display-names' import {TimeElapsed} from '#/view/com/util/TimeElapsed' import {UserAvatar} from '#/view/com/util/UserAvatar' @@ -103,11 +104,16 @@ function ChatListItemReady({ setShowActions(true) }, []) - const onPress = useCallback(() => { + const onPress = useCallback(async () => { + if (convo.unreadCount > 0) { + // We only increment the badge once per conversation, so even if there are multiple unreads + // we only want to decrement by one. + decrementBadgeCount(1) + } navigation.push('MessagesConversation', { conversation: convo.id, }) - }, [convo.id, navigation]) + }, [convo.id, convo.unreadCount, navigation]) return (