如何获取Firebase令牌?

时间:2017-05-11 05:54:25

标签: ios firebase swift3 firebase-cloud-messaging

如何获得令牌值?我正在使用firebase,swift3,xcode8

令牌值为零。

import UIKit
import Firebase

class TestViewController: UIViewController {
// FCM Messaging test
    override func viewDidLoad() {
       super.viewDidLoad()

       let token = FIRInstanceID.instanceID().token()

       print("Token is \(token)")
}

控制台日志"Token is nil"

1 个答案:

答案 0 :(得分:0)

我也有同样的问题,我通过添加以下代码

解决了这个问题
     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
    {
       NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification), name: kFIRInstanceIDTokenRefreshNotification, object: nil)
    }

然后

func tokenRefreshNotification(notification: NSNotification) {
     //    NOTE: It can be nil here
           if let token = FIRInstanceID.instanceID().token() {
              refreshedToken = token
                print("InstanceID token: \(refreshedToken)")

        }
    }

如果您要获得令牌,请检查控制台。