What should I do that for changing or requesting the token in firebase? the unique token generated by firebase on the basis of device information.
-
I'm sorry that you didn't like my edit to your question. However, please note that the `device-instance-id` tag is about the Windows Device Instance ID, and that it has nothing to do with the Firebase InstanceID. May I suggest you the `instanceid` tag? Please note that, as of now, there are only 2 questions that are using the `device-instance-id` tag in a wrong way, and this is one of them. – gog Jul 08 '19 at 10:15
5 Answers
Now i got my answer after facing many troubles for generating new or change token of firebase for push notification.
1) Delete old Firebase token
let instance = FIRInstanceID.instanceID()
_ = FIRInstanceID.delete(instance)
FIRInstanceID.instanceID().delete { (err:Error?) in
if err != nil{
print(err.debugDescription);
} else {
print("Token Deleted");
}
}
2) Request new Firebase token
if let token = FIRInstanceID.instanceID().token() {
print("Token \(token) fetched");
} else {
print("Unable to fetch token");
}
FIRMessaging.messaging().connect { (error) in
if (error != nil) {
print("Error connecting to FCM. \(error.debugDescription)")
} else {
print("Connected to FCM.")
}
}
UPDATE FOR SWIFT 4 & Firebase 4.8.2 (Follow simple two steps)
1) Delete old Token
let instance = InstanceID.instanceID()
instance.deleteID { (error) in
print(error.debugDescription)
}
2) Request for new token
if let token = InstanceID.instanceID().token() {
print("Token : \(token)");
} else {
print(“Error: unable to fetch token");
}
Messaging.messaging().shouldEstablishDirectChannel = true
You can get updated token in MessagingDelegate method didReceiveRegistrationToken
and in Refresh Token.
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase Token : \(fcmToken)")
}

- 5,905
- 4
- 16
- 32
-
1This "messaging().connect" is needed for renewed token ? – Daniel Arantes Loverde Sep 21 '17 at 13:10
-
yes because there is no way to renew the token, first we have to delete the token and after that requesting for a new token, So "messaging().connect" required. if you will find another way, suggestions are always welcome. – PRAVEEN Sep 22 '17 at 03:57
-
This "delete" is not used anymore, we have to use "shouldEstablishDirectChannel" boolean, and it is not refreshing the token, keep the same. When i resolve it, i will put here. Thanks! – Daniel Arantes Loverde Sep 22 '17 at 14:28
-
for Swift 3: let instance = InstanceID.instanceID() instance.deleteID { (error) in print(error.debugDescription) } – Szekspir Oct 02 '17 at 14:56
-
How can this be done with regular objective C and the older firebase SDK? – Mike Flynn Sep 12 '18 at 17:36
for now InstanceID.instanceID().token()
is deprecated.
You should use this:
let instance = InstanceID.instanceID()
instance.deleteID { (error) in
print(error.debugDescription)
}
instance.instanceID { (result, error) in
if let error = error {
print("Error fetching remote instange ID: \(error)")
} else {
print("Remote instance ID token: \(String(describing: result?.token))")
}
}
Messaging.messaging().shouldEstablishDirectChannel = true
Then in AppDelegate:
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
//Here is your new FCM token
print("Registered with FCM with token:", fcmToken)
}

- 859
- 7
- 12
-
in "instance.instanceID" result?.token return old token. therefore no need to call this method. – Harsh Pipaliya Sep 17 '20 at 10:04
UPDATED FOR FIREBASE MESSAGING 7.3.0
class func regenerateFCM(){
Installations.installations().delete { (err) in
if let err = err {
print(err)
}else{
Installations.installations().authTokenForcingRefresh(true) { (result,err) in
if let result = result {
print(result)
Messaging.messaging().deleteToken { (err) in
if let err = err {
print(err)
}else{
print("FCM TOKEN DELETED")
Messaging.messaging().token { (token, err) in
if let token = token {
print("NEW FCM TOKEN GENERATED")
print(token)
}
if let err = err {
print("ERROR WHILE GENERATING NEW FCM TOKEN")
print(err)
}
}
}
}
}else if let err = err {
print(err)
}
}
}
}
}
UPDATE FOR FIREBASE 8.5.0
Messaging.messaging().deleteToken { err in
if let err = err {
print("Error while generating new FCM Token")
print(err)
}else{
Messaging.messaging().token { token, err in
if let token = token {
print("NEW FCM TOKEN GENERATED")
print(token)
}
}
}
}

- 989
- 8
- 13
-
Did you get it to work? @Shahzaib I got new tokens but they don't work with FCM. Only the initial token works – Tal Zion May 09 '21 at 12:27
Updated Answer for Swift 4, FireBase 4.8.2, FirebaseMessaging (2.0.8)
debugPrint("Existing Token :- \(Messaging.messaging().fcmToken!)")
let instance = InstanceID.instanceID()
instance.deleteID { (error) in
print(error.debugDescription)
}
if let token = InstanceID.instanceID().token() {
print("Token \(token) fetched");
} else {
print("Unable to fetch token");
}
Messaging.messaging().shouldEstablishDirectChannel = true
We receive this updated token in MessagingDelegate method as well as in Refresh Token
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
print("Firebase registration token: \(fcmToken)")
}

- 842
- 1
- 9
- 25
I understand that you want to change or update the firebase token.
Create the following two methods
func registerFirebaseToken() {
if let token = InstanceID.instanceID().token() {
print("FIREBASE: Token \(token) fetched")
} else {
print("FIREBASE: Unable to fetch token");
}
Messaging.messaging().shouldEstablishDirectChannel = true
}
func unregisterFirebaseToken(completion: @escaping (Bool)->()) {
// Delete the Firebase instance ID
InstanceID.instanceID().deleteID { (error) in
if error != nil{
print("FIREBASE: ", error.debugDescription);
completion(false)
} else {
print("FIREBASE: Token Deleted");
completion(true)
}
}
}
Call the
unregisterFirebaseToken(:)
and in the closure check if true then call
registerFirebaseToken()
this will fail for the first time and one of the delegate method will be called i.e.
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
registerFirebaseToken()
}
}
This time
registerFirebaseToken()
will be called again from the delegate method and you will get a new token.

- 2,526
- 3
- 37
- 57
-
Its working at first time only, then not receive notification from firebase console and apps. Permission is allowed and token is refreshed and tried with refresh token. Any idea for push notification are not coming? – Nikunj Jadav Feb 29 '20 at 18:17