通过代码强制更改/更新Firebase通知令牌或实例ID?

时间:2016-10-29 02:00:36

标签: ios swift firebase firebase-cloud-messaging device-instance-id

在firebase中更改或请求令牌应该怎么做? firebase根据设备信息生成的唯一令牌。

5 个答案:

答案 0 :(得分:10)

现在我在面对因为推送通知而生成新的或更改firebase令牌的许多麻烦后得到了答案。

1)删除旧的Firebase令牌

let instance = FIRInstanceID.instanceID()
_ = FIRInstanceID.delete(instance)
FIRInstanceID.instanceID().delete { (err:Error?) in
    if err != nil{
        print(err.debugDescription);
    } else {
        print("Token Deleted");
    }
}

2)申请新的Firebase令牌

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

更新SWIFT 4& Firebase 4.8.2(按照简单的两个步骤)

1)删除旧令牌

let instance = InstanceID.instanceID()
instance.deleteID { (error) in
    print(error.debugDescription)
}

2)请求新令牌

if let token = InstanceID.instanceID().token() {
    print("Token : \(token)");
} else {
    print(“Error: unable to fetch token");
}

Messaging.messaging().shouldEstablishDirectChannel = true

您可以在MessagingDelegate方法didReceiveRegistrationToken和刷新令牌中获取更新的令牌。

func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
    print("Firebase Token :  \(fcmToken)")
}

答案 1 :(得分:2)

  

更新了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
  

我们在MessagingDelegate方法以及Refresh Token

中收到此更新的令牌
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
    print("Firebase registration token: \(fcmToken)")
}

答案 2 :(得分:2)

针对 FIREBASE 消息 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)
                }
            }
        }
    }
}

答案 3 :(得分:0)

我了解您要更改或更新firebase令牌。

创建以下两种方法

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

致电

  

unregisterFirebaseToken(:)

并在闭包中检查是否为true然后调用

  

registerFirebaseToken()

这将首次失败,并且将调用其中一个委托方法,即

extension AppDelegate: MessagingDelegate {
    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
        registerFirebaseToken()
    }
}

这一次

  

registerFirebaseToken()

将从委托方法再次调用,您将获得一个新令牌。

答案 4 :(得分:0)

暂时不推荐使用 i.ID 1 2 3 4 1: 1 32.4 20.7 19.4 15.2 2: 2 NA 20.7 18.8 14.6 3: 3 NA NA 19.4 15.2 4: 4 NA NA NA 15.8

您应该使用此:

InstanceID.instanceID().token()

然后在AppDelegate中:

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