Change/update Firebase notification token or instance id forcefully via code?

岁酱吖の 提交于 2019-11-27 11:44:48

问题


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.


回答1:


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)")
}



回答2:


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)")
}



回答3:


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)
}



回答4:


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.



来源:https://stackoverflow.com/questions/40315345/change-update-firebase-notification-token-or-instance-id-forcefully-via-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!