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); }
Prashanth Debbadwar Sep 29 '16 at 12:51 2016-09-29 12:51
source share