In my React native app previously I implemented Registration and Login with email and password, now i changed that with Mobile Number and verified with OTP. In previously created accounts are getting FCM notifications but new account created with mobile number are not getting notifications.
Tokens are generated from message().getToken()
and also sending to the backend to save that token when registering and login.
This is the function for verifying mobile with OTP and making the decision user to try to log in or registers base upon name if the name is present there then user is login else user is registering himself.
const handleSubmit = async () => {
const fcmToken = await messaging().getToken();
// console.log("On OTP Submission>>", fcmToken);
// const authStatus = await messaging().requestPermission();
// const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL;
// if (!enabled) {
// console.log("Notification permissions not granted");
// }else{
// console.log("Notification permissions granted");
// }
if (otp && (remainingSeconds > 0)) {
// console.log("otp in Submit OTP", otp)
await handleOtp2(phone_number, otp).then(res => {
// console.log("otp res", res)
// if res.data.name !=="" then QRScreen
// else res.data.name === "" then UserNameScreen
if (res.status === 201) {
AsyncStorage.setItem('refreshToken', res.data.token.refresh);
AsyncStorage.setItem("authToken", res.data.token.access);
setModalVisible(true)
setModalInfo("OTP Verification Successful")
const token = res.data.token.access;
const decode = jwt_decode(token)
const { unique_id, name } = decode;
// console.log("res of right OTP>>>>",decode);
console.log("FCM TOKEN & TOKEN>>>>", fcmToken, token);
checkDevice(token, fcmToken, Platform.OS).then(res => console.log("RESPONSE FROM CHECK DEVICE", res))
.catch(error => {
console.log(error)
})
if (name) {
checkUserPhoneNumber(unique_id).then(res => {
console.log("check mobile func>>>", res)
// checkDevice(token, fcmToken, Platform.OS).then(res=> console.log("RESPONSE FROM CHECK DEVICE",res))
// .catch(error => {
// console.log(error)
// })
if (res.status === 200) {
// console.log("check FCM token and Auth Token", { fcmToken, token, decode })
// checkDevice(token, fcmToken, Platform.OS).catch(error => {
// console.log(error)
// })
// setModalVisible(false);
// authContext.retriveToken();
setTimeout(() => {
setModalVisible(false);
authContext.retriveToken();
}, 1000)
}
else if (res.response.status === 400) {
props.navigation.navigate('signupScreen')
}
}).catch(err => {
console.log(err)
props.navigation.navigate('signupScreen')
})
}
else {
// else if ((name === null && email === null)) {
// checkDevice(token, fcmToken, Platform.OS).catch(error => {
// console.log(error)
// })
setTimeout(() => {
setModalVisible(false)
props.navigation.reset({
index: 0,
routes: [{
name: 'nameInputScreen',
params: { unique_id: unique_id }
}],
})
}, 1000)
}
}
else if (res.response.status === 400) {
setModalVisible(true)
setModalInfo(res.response.data.error)
} else {
let data = res.response.data.error;
setModalVisible(true)
setModalInfo("Something went wrong")
console.log("error try again")
}
}).catch(err => {
console.log(err)
if (err.response.status === 400) {
setModalVisible(true)
setModalInfo(err.response.data.error)
} else {
setModalVisible(true)
setModalInfo("Something went wrong")
}
})
} else {
setModalVisible(true)
setModalInfo("Invalid OTP")
}
}