How to start the FCM authentication service only after starting a specific action?

Suppose I have LoginActivity where a user can either register or log in with existing credentials. I do not want the FirebaseInstanceIdService generate a token if the user is not logged in and the MainActivity application is not running.

thank

+10
android firebase-cloud-messaging google-cloud-messaging
May 30 '16 at 3:35
source share
3 answers

You cannot block FirebaseInstanceIdService.onTokenRefresh() from calling until the user logs in.

What you can do in your case:

  • In FirebaseInstanceIdService.onTokenRefresh() ignore the event if the user is not logged in
  • When the user logs in to FirebaseInstanceId.getToken() and != null will call onTokenRefresh() (or directly your logic) manually.

This way you can process the token when the user is logged in, and if the token is unavailable (or rotated), you will later receive the onTokenRefresh() event.

Update (July 3, 2017) . In the comments, the reader recalled that FirebaseInstanceIdService.onTokenRefresh() can be called after the user logs in.

It is right. When a user logs in, getToken() can still return null if onTokenRefresh() not been called before.

You need to use this case in your application. Most likely, the user can use the application in any case, but you cannot send a push notification until you receive a token.

When onTokenRefresh() finally called if the user logs in before you can associate the token with the user.

+13
May 30 '16 at 20:28
source share

Sorry, but this is not possible. FirebaseInstanceIdService starts automatically when the application starts and generates Token . Keep in mind that this is related to the Instance app. Not with one specific user. If you are trying to save Token with one specific user (i.e. when the user is logged in, you will save his Token on the db server for push notification of this user). If you do this, what you will encounter in the future is that if two users use the same Instance application, then the push notification may be redirected to the wrong user. I hope you understand my point.

+2
Jun 01 '16 at 19:07
source share

I support one flag in a shared pref that indicates whether a gcm token is sent to the server or not. In the Splash window, every time I call one sendDevicetokenToServer method. This method checks if the user ID is empty and gcm send the status, and then send the token to the server.

  public static void sendRegistrationToServer(final Context context) { if(Common.getBooleanPerf(context,Constants.isTokenSentToServer,false) || Common.getStringPref(context,Constants.userId,"").isEmpty()){ return; } String token = FirebaseInstanceId.getInstance().getToken(); String userId = Common.getUserId(context); if(!userId.isEmpty()) { HashMap<String, Object> reqJson = new HashMap<>(); reqJson.put("deviceToken", token); ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class); Call<JsonElement> call = apiService.updateDeviceToken(reqJson,Common.getUserId(context),Common.getAccessToken(context)); call.enqueue(new Callback<JsonElement>() { @Override public void onResponse(Call<JsonElement> call, Response<JsonElement> serverResponse) { try { JsonElement jsonElement = serverResponse.body(); JSONObject response = new JSONObject(jsonElement.toString()); if(context == null ){ return; } if(response.getString(Constants.statusCode).equalsIgnoreCase(Constants.responseStatusSuccess)) { Common.saveBooleanPref(context,Constants.isTokenSentToServer,true); } }catch (Exception e){ e.printStackTrace(); } } @Override public void onFailure(Call<JsonElement> call, Throwable throwable) { Log.d("", "RetroFit2.0 :getAppVersion: " + "eroorrrrrrrrrrrr"); Log.e("eroooooooorr", throwable.toString()); } }); } } 

In class MyFirebaseInstanceIDService

  @Override public void onTokenRefresh() { // Get updated InstanceID token. String refreshedToken = FirebaseInstanceId.getInstance().getToken(); Log.d(TAG, "Refreshed token: " + refreshedToken); // If you want to send messages to this application instance or // manage this apps subscriptions on the server side, send the // Instance ID token to your app server. Common.saveBooleanPref(this,Constants.isTokenSentToServer,false); Common.sendRegistrationToServer(this); } 
0
Sep 29 '16 at 12:51
source share



All Articles