我正在努力检索 FCM 令牌,并且我的实现在 iOS 17 上完美运行。但是,当我在 iOS 15 上尝试相同的代码时,我始终收到设备令牌的空值。以下是我的代码的相关部分。我是否需要更改较低 iOS 版本的实现?感觉这可能与应用程序跟踪透明度(ATT)权限有关。任何关于为什么会发生这种情况或如何解决它的见解将不胜感激。
const userlogin = async () => {
const deviceType = Platform.OS === 'ios' ? 'IOS' : 'ANDROID';
try {
let fcmToken = await AsyncStorage.getItem('fcmToken');
if (!EMAIL_REGEX.test(email)) {
showError('Please enter a valid email');
updateState({ isloading: false });
} else if (password?.length < 4) {
if (password?.length < 1) {
showError('Please enter password');
updateState({ isloading: false });
} else {
showError('Password should be at least 6 characters long');
updateState({ isloading: false });
}
} else {
let apidata = {
email: email?.toLowerCase(),
password: password,
deviceToken: fcmToken,
deviceType: deviceType,
};
actions.Userlogin(apidata)
.then((res: any) => {
if (!res?.isProfileSetup && res?.tagsId?.length > 0) {
navigation.navigate(navigationString.PRESONALIZESCREEN, {
userData: res,
});
updateState({ isloading: false });
} else if (
(!res?.isProfileSetup && res?.tagsId?.length === 0) ||
res?.tagsId?.length === undefined
) {
navigation.navigate(navigationString.HOBBIESSCREEN);
updateState({ isloading: false });
} else {
updateState({ isloading: false });
// showSuccess('Welcome!');
}
console.log(res, 'User has successfully logged in');
})
.catch((err: any) => {
if (err?.data) showError(err?.data);
updateState({ isloading: false });
});
}
} catch (error) {
updateState({ isloading: false });
console.log(error, 'Error in user login');
}
};
const getFcmToken = async () => {
let fcmToken = await AsyncStorage.getItem('fcmToken');
console.log(fcmToken, 'the old token');
if (!fcmToken) {
try {
const fcmToken = await messaging().getToken();
if (fcmToken) {
console.log(fcmToken, 'the newly generated token');
await AsyncStorage.setItem('fcmToken', fcmToken);
}
} catch (error) {
console.log(error, 'Error in getting FCM token');
}
}
};
export async function requestUserPermission() {
const authStatus = await messaging().requestPermission();
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
console.log('Authorization status:', authStatus);
getFcmToken();
}
}
这是我们遇到的错误
[Error: [messaging/unregistered] You must be registered for remote messages before calling getToken, see messaging().registerDeviceForRemoteMessages().] error in fcmToken.
这意味着 getFcmToken 正在被调用。
该问题涉及 Firebase 库的兼容性问题。如果您需要更多详细信息,请联系我。