Bluesky app fork with some witchin' additions 💫

Use the proper logic on iOS to increment the badge (#4233)

authored by hailey.at and committed by

GitHub 480a4086 c58aedf0

+34 -15
+7 -1
modules/BlueskyNSE/NotificationService.swift
··· 1 1 import UserNotifications 2 + import UIKit 2 3 3 4 let APP_GROUP = "group.app.bsky" 4 5 ··· 31 32 } 32 33 33 34 func mutateWithBadge(_ content: UNMutableNotificationContent) { 34 - content.badge = 1 35 + var count = prefs?.integer(forKey: "badgeCount") ?? 0 36 + count += 1 37 + 38 + // Set the new badge number for the notification, then store that value for using later 39 + content.badge = NSNumber(value: count) 40 + prefs?.setValue(count, forKey: "badgeCount") 35 41 } 36 42 37 43 func mutateWithChatMessage(_ content: UNMutableNotificationContent) {
+4
modules/expo-background-notification-handler/android/src/main/java/expo/modules/backgroundnotificationhandler/ExpoBackgroundNotificationHandlerModule.kt
··· 66 66 AsyncFunction("removeManyFromStringArrayAsync") { forKey: String, strings: Array<String> -> 67 67 NotificationPrefs(appContext.reactContext).removeManyFromStringArray(forKey, strings) 68 68 } 69 + 70 + AsyncFunction("setBadgeCountAsync") { _: Int -> 71 + // This does nothing on Android 72 + } 69 73 } 70 74 }
+6 -1
modules/expo-background-notification-handler/ios/ExpoBackgroundNotificationHandlerModule.swift
··· 10 10 "playSoundQuote": false, 11 11 "playSoundReply": false, 12 12 "playSoundRepost": false, 13 - "mutedThreads": [:] as! [String:[String]] 13 + "mutedThreads": [:] as! [String:[String]], 14 + "badgeCount": 0, 14 15 ] 15 16 16 17 /* ··· 111 112 } 112 113 userDefaults?.setValue(curr, forKey: forKey) 113 114 } 115 + } 116 + 117 + AsyncFunction("setBadgeCountAsync") { (count: Int) in 118 + userDefaults?.setValue(count, forKey: "badgeCount") 114 119 } 115 120 } 116 121 }
+1
modules/expo-background-notification-handler/src/ExpoBackgroundNotificationHandler.types.ts
··· 31 31 forKey: keyof BackgroundNotificationHandlerPreferences, 32 32 value: string[], 33 33 ) => Promise<void> 34 + setBadgeCountAsync: (count: number) => Promise<void> 34 35 } 35 36 36 37 // TODO there are more preferences in the native code, however they have not been added here yet.
+1
modules/expo-background-notification-handler/src/ExpoBackgroundNotificationHandlerModule.web.ts
··· 24 24 removeFromStringArrayAsync: async (_: string, __: string) => {}, 25 25 addManyToStringArrayAsync: async (_: string, __: string[]) => {}, 26 26 removeManyFromStringArrayAsync: async (_: string, __: string[]) => {}, 27 + setBadgeCountAsync: async (_: number) => {}, 27 28 } as ExpoBackgroundNotificationHandlerModule
+13 -11
src/lib/notifications/notifications.ts
··· 7 7 import {SessionAccount, useAgent, useSession} from '#/state/session' 8 8 import {logEvent, useGate} from 'lib/statsig/statsig' 9 9 import {devicePlatform, isNative} from 'platform/detection' 10 + import BackgroundNotificationHandler from '../../../modules/expo-background-notification-handler' 10 11 11 12 const SERVICE_DID = (serviceUrl?: string) => 12 13 serviceUrl?.includes('staging') ··· 108 109 } 109 110 } 110 111 111 - export async function decrementBadgeCount(by: number | 'reset' = 1) { 112 + export async function decrementBadgeCount(by: number) { 112 113 if (!isNative) return 113 114 114 - const currCount = await getBadgeCountAsync() 115 + let count = await getBadgeCountAsync() 116 + count -= by 117 + if (count < 0) { 118 + count = 0 119 + } 115 120 116 - if (by === 'reset') { 117 - await setBadgeCountAsync(0) 118 - return 119 - } 121 + await BackgroundNotificationHandler.setBadgeCountAsync(count) 122 + await setBadgeCountAsync(count) 123 + } 120 124 121 - let newCount = currCount - by 122 - if (newCount < 0) { 123 - newCount = 0 124 - } 125 - await setBadgeCountAsync(newCount) 125 + export async function resetBadgeCount() { 126 + await BackgroundNotificationHandler.setBadgeCountAsync(0) 127 + await setBadgeCountAsync(0) 126 128 }
+2 -2
src/state/queries/notifications/unread.tsx
··· 11 11 import {logger} from '#/logger' 12 12 import {useMutedThreads} from '#/state/muted-threads' 13 13 import {useAgent, useSession} from '#/state/session' 14 - import {decrementBadgeCount} from 'lib/notifications/notifications' 14 + import {resetBadgeCount} from 'lib/notifications/notifications' 15 15 import {useModerationOpts} from '../../preferences/moderation-opts' 16 16 import {truncateAndInvalidate} from '../util' 17 17 import {RQKEY as RQKEY_NOTIFS} from './feed' ··· 119 119 // update & broadcast 120 120 setNumUnread('') 121 121 broadcast.postMessage({event: ''}) 122 - decrementBadgeCount('reset') 122 + resetBadgeCount() 123 123 }, 124 124 125 125 async checkUnread({