Swift FIRMessaging push token token reset

I am currently working with push notifications in my new project, and therefore I am experimenting with creating push tokens. My question is when does this expire? I store each user's tokens in the Firebase database, but I know that these tokens expire / are regenerated. So my question is: what is the best way to keep the token up to date?

I just did it every time the user goes to his profile ?:

let token = FIRInstanceID.instanceID().token() ref.child("Users").child(id).child(pushToken).updateChildValues(["tokenid":token]) 

You see that right now I’m asking the user to allow push notifications immediately after registration, but for some reason it seems that he still generates a token identifier, even though I reject the request. So my question is: how can I update this token? Should I do this in AppDelegate?

Thanks. Help is much appreciated!

0
source share
1 answer

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

+1
source

Source: https://habr.com/ru/post/1268954/


All Articles