Disable badge incrementing for DMs (#4088)

* disable badge increments for dms

* revert decrementing in js for dms

* reset badge on read notifications

* remove some other code

* prevent duplicate notification events
zio/stable
Hailey 2024-05-17 16:05:32 -07:00 committed by GitHub
parent 49314e2d1f
commit d2c81c9d3d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 24 additions and 28 deletions

View File

@ -6,7 +6,7 @@ class NotificationService: UNNotificationServiceExtension {
var prefs = UserDefaults(suiteName: APP_GROUP) var prefs = UserDefaults(suiteName: APP_GROUP)
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
guard var bestAttempt = createCopy(request.content), guard let bestAttempt = createCopy(request.content),
let reason = request.content.userInfo["reason"] as? String let reason = request.content.userInfo["reason"] as? String
else { else {
contentHandler(request.content) contentHandler(request.content)
@ -15,10 +15,9 @@ class NotificationService: UNNotificationServiceExtension {
if reason == "chat-message" { if reason == "chat-message" {
mutateWithChatMessage(bestAttempt) mutateWithChatMessage(bestAttempt)
} } else {
// The badge should always be incremented when in the background
mutateWithBadge(bestAttempt) mutateWithBadge(bestAttempt)
}
contentHandler(bestAttempt) contentHandler(bestAttempt)
} }

View File

@ -35,5 +35,8 @@ class BackgroundNotificationHandler(
remoteMessage.data["sound"] = null remoteMessage.data["sound"] = null
} }
} }
// TODO - Remove this once we have more backend capability
remoteMessage.data["badge"] = null
} }
} }

View File

@ -46,8 +46,9 @@ const DEFAULT_HANDLER_OPTIONS = {
shouldSetBadge: true, shouldSetBadge: true,
} }
// This needs to stay outside the hook to persist between account switches // These need to stay outside the hook to persist between account switches
let storedPayload: NotificationPayload | undefined let storedPayload: NotificationPayload | undefined
let prevDate = 0
export function useNotificationsHandler() { export function useNotificationsHandler() {
const queryClient = useQueryClient() const queryClient = useQueryClient()
@ -58,9 +59,6 @@ export function useNotificationsHandler() {
const {setShowLoggedOut} = useLoggedOutViewControls() const {setShowLoggedOut} = useLoggedOutViewControls()
const closeAllActiveElements = useCloseAllActiveElements() const closeAllActiveElements = useCloseAllActiveElements()
// Safety to prevent double handling of the same notification
const prevDate = React.useRef(0)
React.useEffect(() => { React.useEffect(() => {
if (!isAndroid) return if (!isAndroid) return
@ -169,11 +167,10 @@ export function useNotificationsHandler() {
payload.reason === 'chat-message' && payload.reason === 'chat-message' &&
payload.recipientDid === currentAccount?.did payload.recipientDid === currentAccount?.did
) { ) {
const isCurrentConvo = payload.convoId === currentConvoId
return { return {
shouldShowAlert: !isCurrentConvo, shouldShowAlert: payload.convoId !== currentConvoId,
shouldPlaySound: false, shouldPlaySound: false,
shouldSetBadge: !isCurrentConvo, shouldSetBadge: false,
} }
} }
@ -185,10 +182,10 @@ export function useNotificationsHandler() {
const responseReceivedListener = const responseReceivedListener =
Notifications.addNotificationResponseReceivedListener(e => { Notifications.addNotificationResponseReceivedListener(e => {
if (e.notification.date === prevDate.current) { if (e.notification.date === prevDate) {
return return
} }
prevDate.current = e.notification.date prevDate = e.notification.date
logger.debug( logger.debug(
'Notifications: response received', 'Notifications: response received',

View File

@ -113,10 +113,16 @@ export function useRequestNotificationsPermission() {
) )
} }
export async function decrementBadgeCount(by = 1) { export async function decrementBadgeCount(by: number | 'reset' = 1) {
if (!isNative) return if (!isNative) return
const currCount = await getBadgeCountAsync() const currCount = await getBadgeCountAsync()
if (by === 'reset') {
await setBadgeCountAsync(0)
return
}
let newCount = currCount - by let newCount = currCount - by
if (newCount < 0) { if (newCount < 0) {
newCount = 0 newCount = 0

View File

@ -15,7 +15,6 @@ import {useCurrentConvoId} from '#/state/messages/current-convo-id'
import {useModerationOpts} from '#/state/preferences/moderation-opts' import {useModerationOpts} from '#/state/preferences/moderation-opts'
import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const' import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const'
import {useAgent, useSession} from '#/state/session' import {useAgent, useSession} from '#/state/session'
import {decrementBadgeCount} from 'lib/notifications/notifications'
export const RQKEY = ['convo-list'] export const RQKEY = ['convo-list']
type RQPageParam = string | undefined type RQPageParam = string | undefined
@ -135,18 +134,10 @@ export function useOnMarkAsRead() {
return useCallback( return useCallback(
(chatId: string) => { (chatId: string) => {
queryClient.setQueryData(RQKEY, (old: ConvoListQueryData) => { queryClient.setQueryData(RQKEY, (old: ConvoListQueryData) => {
return optimisticUpdate(chatId, old, convo => { return optimisticUpdate(chatId, old, convo => ({
// We only want to decrement the badge by one no matter the unread count, since we only increment once per
// sender regardless of message count
if (convo.unreadCount > 0) {
decrementBadgeCount(1)
}
return {
...convo, ...convo,
unreadCount: 0, unreadCount: 0,
} }))
})
}) })
}, },
[queryClient], [queryClient],

View File

@ -119,7 +119,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
// update & broadcast // update & broadcast
setNumUnread('') setNumUnread('')
broadcast.postMessage({event: ''}) broadcast.postMessage({event: ''})
decrementBadgeCount(Math.min(cacheRef.current.unreadCount, 30)) decrementBadgeCount('reset')
}, },
async checkUnread({ async checkUnread({