1

New Firebase token is generated (onTokenRefresh() is called) when The app is run first time. I want to print token when users close the application alltime.(Example; open app and close it- token print, 2. time open and close - token print, 3. time and again.) So I want to print token when every closing. Sorry for my bad English.

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

@Override
public void onTokenRefresh() {
    String token = FirebaseInstanceId.getInstance().getToken();
    registerToken(token);
    Log.d("TOKEN Verildi", token);
}

private void registerToken(String token){
//print code here
}}

Edit: I have added

String token = FirebaseInstanceId.getInstance().getToken();
registerToken(token);

this code in main activity onCreate and after this code

private void registerToken(String token){
//print code here
}}

genereted signed apk successfully but apk not open in my phone. It say Unfortunately (app name) has stopped.

Joe J.
  • 19
  • 1
  • 6
  • There's no really clear moment when an *app* closes. For example: the service will actually continue to run when the main activity exits. In general, see http://stackoverflow.com/questions/10387774/detect-when-application-is-closed – Frank van Puffelen Nov 15 '16 at 03:59
  • Thank you @frank-van-puffelen . I have a one more question. How can I print token when application starting every time? That's my print code but that code print token only first time. Thank you private void registerToken(String token){ //print code here }} – Joe J. Nov 15 '16 at 04:38
  • On a second start of the app, the token isn't (usually) refreshed. So you should print it from somewhere else than the `onTokenRefresh()`. For example from within the `onCreate()` of your main activity. – Frank van Puffelen Nov 15 '16 at 04:47
  • I got some error when I add this code on my main activity. public void onTokenRefresh() { String token = FirebaseInstanceId.getInstance().getToken(); registerToken(token); Log.d("TOKEN Verildi", token); } private void registerToken(String token){ //print code here }} How can I add this to my main activity without error? Sorry for the questions. I'm new on android. @FrankvanPuffelen – Joe J. Nov 15 '16 at 04:57
  • 1
    That depends on the error you're getting. Please edit your question (there's a handy edit link right under it) to include both the error you get and the code that produces that error. – Frank van Puffelen Nov 15 '16 at 05:09
  • I have updated my post @FrankvanPuffelen – Joe J. Nov 15 '16 at 19:19

0 Answers0