Skip to content

Commit

Permalink
Disable badge incrementing for DMs (#4088)
Browse files Browse the repository at this point in the history
* disable badge increments for dms

* revert decrementing in js for dms

* reset badge on read notifications

* remove some other code

* prevent duplicate notification events
  • Loading branch information
haileyok authored May 17, 2024
1 parent 49314e2 commit d2c81c9
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 28 deletions.
7 changes: 3 additions & 4 deletions modules/BlueskyNSE/NotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class NotificationService: UNNotificationServiceExtension {
var prefs = UserDefaults(suiteName: APP_GROUP)

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
else {
contentHandler(request.content)
Expand All @@ -15,11 +15,10 @@ class NotificationService: UNNotificationServiceExtension {

if reason == "chat-message" {
mutateWithChatMessage(bestAttempt)
} else {
mutateWithBadge(bestAttempt)
}

// The badge should always be incremented when in the background
mutateWithBadge(bestAttempt)

contentHandler(bestAttempt)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ class BackgroundNotificationHandler(
remoteMessage.data["sound"] = null
}
}

// TODO - Remove this once we have more backend capability
remoteMessage.data["badge"] = null
}
}
15 changes: 6 additions & 9 deletions src/lib/hooks/useNotificationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ const DEFAULT_HANDLER_OPTIONS = {
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 prevDate = 0

export function useNotificationsHandler() {
const queryClient = useQueryClient()
Expand All @@ -58,9 +59,6 @@ export function useNotificationsHandler() {
const {setShowLoggedOut} = useLoggedOutViewControls()
const closeAllActiveElements = useCloseAllActiveElements()

// Safety to prevent double handling of the same notification
const prevDate = React.useRef(0)

React.useEffect(() => {
if (!isAndroid) return

Expand Down Expand Up @@ -169,11 +167,10 @@ export function useNotificationsHandler() {
payload.reason === 'chat-message' &&
payload.recipientDid === currentAccount?.did
) {
const isCurrentConvo = payload.convoId === currentConvoId
return {
shouldShowAlert: !isCurrentConvo,
shouldShowAlert: payload.convoId !== currentConvoId,
shouldPlaySound: false,
shouldSetBadge: !isCurrentConvo,
shouldSetBadge: false,
}
}

Expand All @@ -185,10 +182,10 @@ export function useNotificationsHandler() {

const responseReceivedListener =
Notifications.addNotificationResponseReceivedListener(e => {
if (e.notification.date === prevDate.current) {
if (e.notification.date === prevDate) {
return
}
prevDate.current = e.notification.date
prevDate = e.notification.date

logger.debug(
'Notifications: response received',
Expand Down
8 changes: 7 additions & 1 deletion src/lib/notifications/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,16 @@ export function useRequestNotificationsPermission() {
)
}

export async function decrementBadgeCount(by = 1) {
export async function decrementBadgeCount(by: number | 'reset' = 1) {
if (!isNative) return

const currCount = await getBadgeCountAsync()

if (by === 'reset') {
await setBadgeCountAsync(0)
return
}

let newCount = currCount - by
if (newCount < 0) {
newCount = 0
Expand Down
17 changes: 4 additions & 13 deletions src/state/queries/messages/list-converations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {useCurrentConvoId} from '#/state/messages/current-convo-id'
import {useModerationOpts} from '#/state/preferences/moderation-opts'
import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const'
import {useAgent, useSession} from '#/state/session'
import {decrementBadgeCount} from 'lib/notifications/notifications'

export const RQKEY = ['convo-list']
type RQPageParam = string | undefined
Expand Down Expand Up @@ -135,18 +134,10 @@ export function useOnMarkAsRead() {
return useCallback(
(chatId: string) => {
queryClient.setQueryData(RQKEY, (old: ConvoListQueryData) => {
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,
unreadCount: 0,
}
})
return optimisticUpdate(chatId, old, convo => ({
...convo,
unreadCount: 0,
}))
})
},
[queryClient],
Expand Down
2 changes: 1 addition & 1 deletion src/state/queries/notifications/unread.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
// update & broadcast
setNumUnread('')
broadcast.postMessage({event: ''})
decrementBadgeCount(Math.min(cacheRef.current.unreadCount, 30))
decrementBadgeCount('reset')
},

async checkUnread({
Expand Down

0 comments on commit d2c81c9

Please sign in to comment.