Change/update 通过代码强制使用 Firebase 通知令牌或实例 ID?
Change/update Firebase notification token or instance id forcefully via code?
我应该如何更改或请求 firebase 中的令牌?
firebase 根据设备信息生成的唯一令牌。
在为推送通知生成新的或更改的 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)")
}
我了解到您想更改或更新 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(:)
并在闭包中检查是否为真然后调用
registerFirebaseToken()
这将是第一次失败,并且将调用其中一个委托方法,即
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
registerFirebaseToken()
}
}
这次
registerFirebaseToken()
将从委托方法中再次调用,您将获得一个新令牌。
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)")
}
目前 InstanceID.instanceID().token()
已弃用。
你应该使用这个:
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
然后在 AppDelegate 中:
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
//Here is your new FCM token
print("Registered with FCM with token:", fcmToken)
}
已针对 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)
}
}
}
}
}
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)
}
}
}
}
我应该如何更改或请求 firebase 中的令牌? firebase 根据设备信息生成的唯一令牌。
在为推送通知生成新的或更改的 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)")
}
我了解到您想更改或更新 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(:)
并在闭包中检查是否为真然后调用
registerFirebaseToken()
这将是第一次失败,并且将调用其中一个委托方法,即
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
registerFirebaseToken()
}
}
这次
registerFirebaseToken()
将从委托方法中再次调用,您将获得一个新令牌。
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)")
}
目前 InstanceID.instanceID().token()
已弃用。
你应该使用这个:
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
然后在 AppDelegate 中:
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
//Here is your new FCM token
print("Registered with FCM with token:", fcmToken)
}
已针对 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)
}
}
}
}
}
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)
}
}
}
}