diff --git a/assets/translations/en.json b/assets/translations/en.json index 973cb97c..3f76c7d3 100644 --- a/assets/translations/en.json +++ b/assets/translations/en.json @@ -490,7 +490,8 @@ "title": "Log in with your polito.it credentials", "unsupportedUserType": "User type not supported: only students can log in to PoliTO Students.", "usernameLabel": "Student ID", - "usernameLabelAccessibility": "Enter your username here" + "usernameLabelAccessibility": "Enter your username here", + "fcmUnsupported": "An error occurred during push notifications setup. This problem can be caused by the lack of Google Play Services on this device. It is possible to keep using this app, however push notifications won't be received." }, "messageScreen": { "backTitle": "Archive", diff --git a/assets/translations/it.json b/assets/translations/it.json index 0f78de2b..0e988c3f 100644 --- a/assets/translations/it.json +++ b/assets/translations/it.json @@ -490,7 +490,8 @@ "title": "Accedi con le tue credenziali polito.it", "unsupportedUserType": "Tipo utente non supportato: PoliTO Students è accessibile solamente agli studenti.", "usernameLabel": "Matricola", - "usernameLabelAccessibility": "Inserisci qui il tuo username" + "usernameLabelAccessibility": "Inserisci qui il tuo username", + "fcmUnsupported": "Si è verificato un errore durante l'attivazione delle notifiche push. Questo problema può essere causato dall'assenza dei servizi Google su questo dispositivo. È possibile continuare ad utilizzare l'app ma non si riceveranno notifiche push." }, "messageScreen": { "backTitle": "Archivio", diff --git a/src/core/queries/authHooks.ts b/src/core/queries/authHooks.ts index 2f06fc16..5c8627fc 100644 --- a/src/core/queries/authHooks.ts +++ b/src/core/queries/authHooks.ts @@ -1,4 +1,4 @@ -import { Platform } from 'react-native'; +import { Alert, Platform } from 'react-native'; import DeviceInfo from 'react-native-device-info'; import Keychain from 'react-native-keychain'; @@ -6,6 +6,8 @@ import { AuthApi, LoginRequest, SwitchCareerRequest } from '@polito/api-client'; import messaging from '@react-native-firebase/messaging'; import { useMutation, useQueryClient } from '@tanstack/react-query'; +import { t } from 'i18next'; + import { isEnvProduction } from '../../utils/env'; import { pluckData } from '../../utils/queries'; import { useApiContext } from '../contexts/ApiContext'; @@ -17,6 +19,18 @@ const useAuthClient = (): AuthApi => { return new AuthApi(); }; +async function getFcmToken(): Promise { + if (!isEnvProduction) return undefined; + + try { + return await messaging().getToken(); + } catch (_) { + Alert.alert(t('common.error'), t('loginScreen.fcmUnsupported')); + } + + return undefined; +} + export const useLogin = () => { const authClient = useAuthClient(); const { refreshContext } = useApiContext(); @@ -32,7 +46,7 @@ export const useLogin = () => { DeviceInfo.getManufacturer(), DeviceInfo.getBuildNumber(), DeviceInfo.getVersion(), - isEnvProduction ? messaging().getToken() : undefined, + getFcmToken(), ]) .then( ([