49

I am working with swift and firebase. Previously I was using following method to get firebase token which then I was using to store into database to send notifications.

InstanceID.instanceID().token()

Now this method is showing as deprecated since i have updated my firebase.

'token()' is deprecated: Use instanceIDWithHandler: instead.

I don't know how to use instanceIDWithHandler i have tried following but don't know how to get token.

func instanceID(handler: @escaping InstanceIDResultHandler){

    }

Please help. Thank you in advance.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Deepak
  • 1,030
  • 2
  • 10
  • 21
  • Are you asking about [this](https://firebase.google.com/docs/cloud-messaging/ios/client#fetching-the-current-registration-token)? – André Kool Jun 20 '18 at 09:44

4 Answers4

66

Fetching the current registration token

Registration tokens are delivered via the method messaging:didReceiveRegistrationToken:. This method is called generally once per app start with an FCM token. When this method is called, it is the ideal time to:

  • If the registration token is new, send it to your application server.
  • Subscribe the registration token to topics. This is required only for new subscriptions or for situations where the user has re-installed the app.

You can retrieve the token directly using instanceIDWithHandler:. This callback provides an InstanceIDResult, which contains the token. A non null error is provided if the InstanceID retrieval failed in any way.

You should import FirebaseInstanceID

  import FirebaseInstanceID

objective C

on your getTokenMethod

[[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result,
                                                NSError * _Nullable error) {
    if (error != nil) {
        NSLog(@"Error fetching remote instance ID: %@", error);
    } else {
        NSLog(@"Remote instance ID token: %@", result.token);
    }
}];

Swift

InstanceID.instanceID().instanceID { result, error in
    if let error = error {
        print("Error fetching remote instange ID: \(error)")
    } else if let result = result {
        print("Remote instance ID token: \(result.token)")
    }
}

Update

InstanceID is now deprecated. Try

Messaging.messaging().token { token, error in
   // Check for error. Otherwise do what you will with token here
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • this solution is working perfect thank you Sir. I want to know where did you get this answer or where i can learn such things. When I read firebase documentation this is what i found [link](https://ibb.co/d4ahqT) which was not useful at all or I was unable to understand it. – Deepak Jun 22 '18 at 07:03
  • 1
    @user9496018 the docs with this information are here: https://firebase.google.com/docs/cloud-messaging/ios/first-message – Deemoe Aug 03 '18 at 19:09
  • 6
    Some of our users are having problem with this new solution, the lambda never returns the result. – Pedro Paulo Amorim Jan 24 '19 at 17:10
  • 3
    same as @PedroPauloAmorim :( – MattBlack Feb 04 '19 at 13:17
  • 1
    same problem, the lambda never return the result! – ios_dotnet_superuser May 22 '19 at 10:26
  • the **result** will be of type **InstanceIDResult**. To the Strings from it use **let newToken = result.token** and **let instanceId = result.instanceID** – Lance Samaria Jun 30 '19 at 12:42
  • lambda hangs for me when i configure firebase using SEGFirebaseIntegrationFactory instead of FirebaseApp.configure() – nb07 Sep 04 '19 at 18:22
  • make sure to add: import FirebaseInstanceID https://stackoverflow.com/a/58641464/4833705 – Lance Samaria May 06 '20 at 09:21
  • As instanceId is deprecated, now you can use Messaging.messaging().token { token, error in // Check for error. Otherwise do what you will with token here } – Muaaz Ahmed Jan 04 '22 at 12:13
55

InstanceID is now deprecated. Try

Messaging.messaging().token { token, error in
   // Check for error. Otherwise do what you will with token here
}

See Documentation on Fetching the current registration token

Andrew
  • 689
  • 5
  • 6
8

Here the solution

The problem is that FirebaseInstanceID has recently become deprecated

Old

InstanceID.instanceID().instanceID {result, _ in

New

Messaging.messaging().token { (token, _) in

You can replace it like above, it working

Check below link

https://medium.com/nerd-for-tech/how-to-solve-capacitor-error-no-such-module-firebaseinstanceid-9c142933b589

0

Update for Objective-C

[[FIRMessaging messaging] tokenWithCompletion:^(NSString * _Nullable token, NSError * _Nullable error) {
    if (error != nil) {
        NSLog(@"Error fetching remote instance ID: %@", error);
    } else {
        NSLog(@"Remote instance ID token: %@", token);
    }
}];