Skip to content

Commit

Permalink
Fix fcm token update onto patchClient (#558)
Browse files Browse the repository at this point in the history
* fix(notifications): update FCM token on app startup only if online

* fix(notifications): optimize update logic in useEffect for better dependency handling

* fix(notifications): add TODO, and correct mutation
  • Loading branch information
FabrizioCostaMedich authored Jan 28, 2025
1 parent bdaf794 commit ce66590
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
16 changes: 14 additions & 2 deletions src/core/components/AppContent.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { useEffect } from 'react';
import { useEffect, useState } from 'react';

import { useQueryClient } from '@tanstack/react-query';

import { useApiContext } from '../contexts/ApiContext';
import { usePreferencesContext } from '../contexts/PreferencesContext';
import { useOfflineDisabled } from '../hooks/useOfflineDisabled.ts';
import { MigrationService } from '../migrations/MigrationService';
import { useUpdateAppInfo } from '../queries/authHooks.ts';
import { GuestNavigator } from './GuestNavigator';
import { RootNavigator } from './RootNavigator';

export const AppContent = () => {
const { isLogged } = useApiContext();

const { mutateAsync: updateAppInfo } = useUpdateAppInfo();
const [clientPatchOk, setClientPatchOk] = useState(false);
const preferences = usePreferencesContext();
const queryClient = useQueryClient();
const isOffline = useOfflineDisabled();

useEffect(() => {
if (clientPatchOk || !isLogged || isOffline) return;
// TODO handle suggestUpdate field in new version in UpdateAppInfo response
updateAppInfo()
.then(() => setClientPatchOk(true))
.catch(console.warn);
}, [isLogged, isOffline, updateAppInfo, clientPatchOk]);

useEffect(() => {
MigrationService.migrateIfNeeded(preferences, queryClient);
Expand Down
4 changes: 1 addition & 3 deletions src/core/hooks/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ export const useInitFirebaseMessaging = () => {
const { mutate: updateAppInfo } = useUpdateAppInfo();

if (isEnvProduction) {
messaging().onTokenRefresh(fcmRegistrationToken => {
updateAppInfo(fcmRegistrationToken);
});
messaging().onTokenRefresh(updateAppInfo);
}

useEffect(() => {
Expand Down
8 changes: 5 additions & 3 deletions src/core/queries/authHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,19 @@ export const useUpdateAppInfo = () => {
const authClient = useAuthClient();

return useMutation({
mutationFn: async (fcmRegistrationToken: string) => {
mutationFn: async (fcmToken: string | void) => {
// mutation requires a variable, an undefined string is not accepted
return Promise.all([
DeviceInfo.getBuildNumber(),
DeviceInfo.getVersion(),
]).then(([buildNumber, appVersion]) => {
fcmToken || getFcmToken(),
]).then(([buildNumber, appVersion, fcmRegistrationToken]) => {
const dto: AppInfoRequest = {
buildNumber,
appVersion,
fcmRegistrationToken,
};
authClient.appInfo({
return authClient.appInfo({
appInfoRequest: dto,
});
});
Expand Down

0 comments on commit ce66590

Please sign in to comment.