To properly store the token in your database, you need to handle two cases:
- when starting the application
- when the token is updated
First, getting the current token in app / main view starts by calling FirebaseInst FIRInstanceID.instanceID().token() in, for example, viewDidLoad :
if let token = FIRInstanceID.instanceID().token() { ref.child("Users").child(id).child(pushToken).updateChildValues(["tokenid":token]) }
Since the current token has not yet been created, you need to process nil in this code.
Then also a monitor for changes in the token by implementing tokenRefreshNotification :
func tokenRefreshNotification(notification: NSNotification) { if let refreshedToken = FIRInstanceID.instanceID().token() { ref.child("Users").child(id).child(pushToken).updateChildValues(["tokenid":refreshedToken]) } }
Also see this answer, which I gave a similar use case for Android: Update Firebase instance id when updating application
source share