0

I am trying to get the device token once the person has logged into the app and send back the device token to our server. The login process is done in async. According to this post , you can get the token in activities but is it possible to get in async? I have tried to put the code in async but does not work. (I changed the MainActivity.this to (Activity)mycontext, where I have defined mycontext as a Context)

FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener( (Activitiy) mycontext,  new OnSuccessListener<InstanceIdResult>() {
 @Override
 public void onSuccess(InstanceIdResult instanceIdResult) {
       String newToken = instanceIdResult.getToken();
       Log.e("newToken",newToken);

 }
});

Thank you very much

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
ehku
  • 99
  • 2
  • 9
  • " I have tried to put the code in async" What do you mean by this? – Frank van Puffelen Jan 18 '19 at 01:46
  • If I understood your requirements correctly, you want to get Firebase token in AysncTask right? Why don't you get token in Activity itself and pass it to AsyncTask as an argument? – Shashanth Jan 18 '19 at 04:51
  • @Shashanth Yes that is what I mean and I have used your method and it works! So simple yet effective! Thanks! – ehku Jan 18 '19 at 08:24
  • Glad to hear that! Post a [self-answer](https://stackoverflow.com/help/self-answer) so that we can close this thread as complete. :) – Shashanth Jan 18 '19 at 08:57

1 Answers1

0

thanks to Shashanth , the solution is just to get the token in one activity and pass that token value to async

ehku
  • 99
  • 2
  • 9