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
This commit is contained in:
Elias Nahum
2022-12-05 12:00:09 +02:00
committed by GitHub
parent 7717fea599
commit dc294eddd3
2 changed files with 9 additions and 0 deletions

View File

@@ -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<UnreadMessages>({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;

View File

@@ -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)
}
}