Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

swift - Token Mismatch using Phone Number Authentication - iOS

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 :

  1. Ensure I had both a valid development and production APNS certificate uploaded to the Firebase Dashboard, under 'Project Settings' > 'Cloud Messaging'. (My APNS certificate is valid till next year).
  2. In Xcode, in the .entitlements file, make sure the APS Environment value is set to either 'development' or 'production', depending on your testing situation. (I also checked).
  3. Finally (this is what I was missing), check inside your AppDelegate.swift and inside the method for 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...