Now I got my answer after I encountered many problems related to generating new or changed tokens in firebase for push notifications.
1) Delete the 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 a 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 and Firebase 4.8.2 (follow two simple steps) 👇👇
1) Delete the old token
let instance = InstanceID.instanceID() instance.deleteID { (error) in print(error.debugDescription) }
2) Request a new token
if let token = InstanceID.instanceID().token() { print("Token : \(token)"); } else { print("Error: unable to fetch token"); } Messaging.messaging().shouldEstablishDirectChannel = true
The updated token can be obtained in the method MessagingDelegate didReceiveRegistrationToken
and in didReceiveRegistrationToken
Refresh.
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { print("Firebase Token : \(fcmToken)") }
source share