Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 798
PRAVEEN Avatar asked Oct 29 '16 02:10

PRAVEEN


People also ask

Can you change Firebase tokens?

To be specific,The registration token may change when: The app deletes Instance ID. The app is restored on a new device. The user uninstall/reinstall the app.

Does FCM token change on app update?

yes it does not change token upon updating.

How long FCM Token expired?

It doesn't expire though. It renews itself if one of the following happens. According to https://firebase.google.com/docs/cloud-messaging/android/client: -The app deletes Instance ID.

What is a FCM push token?

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. This is the token that you must include in targeted send requests from the API, or add to topic subscriptions for targeting topics.


2 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)")
}
like image 151
PRAVEEN Avatar answered Oct 16 '22 08:10

PRAVEEN


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)
                }
            }
        }
    }
like image 23
Shahzaib Qureshi Avatar answered Oct 16 '22 08:10

Shahzaib Qureshi