I am trying to login account using phone number authentication using firebase.
Initially, i deployed the app with my device and it works fine. But when i tried to deploy app to another device and then it throws me error Token Mismatch
.
I have searched several answers in stackoverflow
and then i found this answer and i followed but it didn't work for me.
I have checked following :
didRegisterForRemoteNotificationsWithDeviceToken
, change the value from .sandbox
to .prod
, or to .unknown
to let the app bundle determine which token type to use, based on your provisioning profile.This 3rd i also changed
let token = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
print("==== This is device token ",token)
let data = Data(token.utf8)
Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown)
But still when i run this app on another device which it always throws me that error.
But when i comment this line of code // Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown)
and then i run the app after that i uncomment that line of code Auth.auth().setAPNSToken(data, type: AuthAPNSTokenType.unknown)
and then i run the app again and finally it works. But sadly, when i run another iOS device it still gives me error.
I wonder why?
Follow steps
1) Import Firebase and FirebaseAuth
import Firebase
import FirebaseAuth
2) In didFinishLaunchingWithOptions Configure firebase.
FirebaseApp.configure()
3) Write these two func in AppDelegate.
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let firebaseAuth = Auth.auth()
firebaseAuth.setAPNSToken(deviceToken, type: AuthAPNSTokenType.prod)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let firebaseAuth = Auth.auth()
if (firebaseAuth.canHandleNotification(userInfo)){
print(userInfo)
return
}
}
4) In your ViewController class, repeat step first and write code for send OTP on Mobile Number, which you want.
@IBAction func sendCodeAction(_ sender: Any) {
let ugrMgr = UserManager.userManager
let phoneNumber = Auth.auth().currentUser?.phoneNumber
print(phoneNumber!)
print(ugrMgr.mobile!)
PhoneAuthProvider.provider().verifyPhoneNumber("+91" + ugrMgr.mobile!, uiDelegate: nil) { (verificationID, error) in
if let error = error {
print(error.localizedDescription)
mainInstance.ShowAlertWithError(error.localizedDescription as NSString, msg: error.localizedDescription as NSString)
return
}
self.verificationID = verificationID
}
}