From dc294eddd3025405206aafa63a45a26103ed965a Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Mon, 5 Dec 2022 12:00:09 +0200 Subject: [PATCH] Update icon badge on Notification (#6823) * Update icon badge when receiving a notification while the app is in the foreground * update app badge when sending the app to the background --- app/screens/home/tab_bar/home.tsx | 8 ++++++++ ios/NotificationService/NotificationService.swift | 1 + 2 files changed, 9 insertions(+) diff --git a/app/screens/home/tab_bar/home.tsx b/app/screens/home/tab_bar/home.tsx index 9d09821167..c029a53e46 100644 --- a/app/screens/home/tab_bar/home.tsx +++ b/app/screens/home/tab_bar/home.tsx @@ -10,6 +10,7 @@ import CompassIcon from '@components/compass_icon'; import {BOTTOM_TAB_ICON_SIZE} from '@constants/view'; import {subscribeAllServers} from '@database/subscription/servers'; import {subscribeUnreadAndMentionsByServer, UnreadObserverArgs} from '@database/subscription/unreads'; +import {useAppState} from '@hooks/device'; import NativeNotification from '@notifications'; import {changeOpacity} from '@utils/theme'; @@ -41,6 +42,7 @@ const style = StyleSheet.create({ const Home = ({isFocused, theme}: Props) => { const [total, setTotal] = useState({mentions: 0, unread: false}); + const appState = useAppState(); const updateTotal = () => { let unread = false; @@ -119,6 +121,12 @@ const Home = ({isFocused, theme}: Props) => { }; }, []); + useEffect(() => { + if (appState === 'background') { + updateTotal(); + } + }, [appState]); + let unreadStyle; if (total.mentions) { unreadStyle = style.mentionsOneDigit; diff --git a/ios/NotificationService/NotificationService.swift b/ios/NotificationService/NotificationService.swift index 436b2c2ccd..88d4078316 100644 --- a/ios/NotificationService/NotificationService.swift +++ b/ios/NotificationService/NotificationService.swift @@ -45,6 +45,7 @@ class NotificationService: UNNotificationServiceExtension { if (preferences.object(forKey: "ApplicationIsForeground") as? String != "true") { Network.default.fetchAndStoreDataForPushNotification(bestAttemptContent, withContentHandler: contentHandler) } else if let contentHandler = contentHandler { + bestAttemptContent.badge = Gekidou.Database.default.getTotalMentions() as NSNumber contentHandler(bestAttemptContent) } }