Commit bd840c83 authored by Varun Patil's avatar Varun Patil

PATCH android_version with fcm_id

parent 0adab2b2
...@@ -34,6 +34,7 @@ import android.widget.Toast; ...@@ -34,6 +34,7 @@ import android.widget.Toast;
import com.google.android.gms.tasks.OnSuccessListener; import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult; import com.google.firebase.iid.InstanceIdResult;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
...@@ -189,31 +190,34 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -189,31 +190,34 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
}); });
} }
private void checkLatestVersion() { /** Get version code we are currently on */
private int getCurrentVersion() {
try { try {
PackageInfo pInfo = this.getPackageManager().getPackageInfo(getPackageName(), 0); PackageInfo pInfo = this.getPackageManager().getPackageInfo(getPackageName(), 0);
final int versionCode = pInfo.versionCode; return pInfo.versionCode;
RetrofitInterface retrofitInterface = getRetrofitInterface();
retrofitInterface.getLatestVersion().enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.isSuccessful()) {
if (response.body().get("version").getAsInt() > versionCode) {
showUpdateSnackBar(response.body().get("message").getAsString());
}
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
}
});
} catch (PackageManager.NameNotFoundException ignored) { } catch (PackageManager.NameNotFoundException ignored) {
return 0;
} }
} }
/** Check for updates in andro.json */
private void checkLatestVersion() {
final int versionCode = getCurrentVersion();
if (versionCode == 0) { return; }
RetrofitInterface retrofitInterface = getRetrofitInterface();
retrofitInterface.getLatestVersion().enqueue(new EmptyCallback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
if (response.isSuccessful()) {
final JsonElement currentVersion = response.body().get("version");
if (currentVersion != null && currentVersion.getAsInt() > versionCode) {
showUpdateSnackBar(response.body().get("message").getAsString());
}
}
}
});
}
private void showUpdateSnackBar(String message) { private void showUpdateSnackBar(String message) {
View parentLayout = findViewById(android.R.id.content); View parentLayout = findViewById(android.R.id.content);
Snackbar.make(parentLayout, message, Snackbar.LENGTH_LONG).setAction("UPDATE", new View.OnClickListener() { Snackbar.make(parentLayout, message, Snackbar.LENGTH_LONG).setAction("UPDATE", new View.OnClickListener() {
...@@ -404,7 +408,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -404,7 +408,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
final String fcmId = instanceIdResult.getToken(); final String fcmId = instanceIdResult.getToken();
RetrofitInterface retrofitInterface = getRetrofitInterface(); RetrofitInterface retrofitInterface = getRetrofitInterface();
retrofitInterface.patchUserMe(getSessionIDHeader(), new UserFCMPatchRequest(fcmId)).enqueue(new Callback<User>() { retrofitInterface.patchUserMe(getSessionIDHeader(), new UserFCMPatchRequest(fcmId, getCurrentVersion())).enqueue(new Callback<User>() {
@Override @Override
public void onResponse(Call<User> call, Response<User> response) { public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
......
...@@ -6,7 +6,11 @@ public class UserFCMPatchRequest { ...@@ -6,7 +6,11 @@ public class UserFCMPatchRequest {
@SerializedName("fcm_id") @SerializedName("fcm_id")
private String userFCMId; private String userFCMId;
public UserFCMPatchRequest(String userFCMId) { @SerializedName("android_version")
private int userAndroidVersion;
public UserFCMPatchRequest(String userFCMId, int userAndroidVersion) {
this.userFCMId = userFCMId; this.userFCMId = userFCMId;
this.userAndroidVersion = userAndroidVersion;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment