We are trying to add the safety net API's to our app. When we test on real devices everything works fine, however when testing on emulators there is no response from the safety net servers. The purpose of the API is to detect emulated devices, so dont know why its not working on an emulator.
Below is the API call:
SafetyNet.getClient(activity).attest(getRequestNonce(nonceData), "<Key-here>").addOnSuccessListener(activity, new OnSuccessListener() {
public void onSuccess(AttestationResponse response) {
String[] jwtParts = response.getJwsResult().split("\\.");
if(jwtParts.length == 3) {
String sharedpreferences = new String(Base64.decode(jwtParts[1], 0));
SharedPreferences editor = context.getSharedPreferences("DecodedPayload", 0);
Editor editor1 = editor.edit();
editor1.putString("decodedPayload", sharedpreferences);
editor1.commit();
Log.d("ContentValues", "The Safety net response is: " + sharedpreferences);
} else {
SharedPreferences sharedpreferences1 = context.getSharedPreferences("DecodedPayload", 0);
Editor editor2 = sharedpreferences1.edit();
editor2.putString("decodedPayload", "CND");
editor2.commit();
Log.d("ContentValues", "The safety net response could not be decoded");
}
}
}).addOnFailureListener(activity, new OnFailureListener() {
public void onFailure(@NonNull Exception e) {
if(e instanceof ApiException) {
ApiException apiException = (ApiException)e;
Log.d("ContentValues", "Error while fetching safety net result: " + ((ApiException)e).getStatusCode() + " " + ((ApiException)e).getStatusMessage());
SharedPreferences sharedpreferences = context.getSharedPreferences("DecodedPayload", 0);
Editor editor = sharedpreferences.edit();
editor.putString("decodedPayload", "ERR");
editor.commit();
Log.d("ContentValues", "The safety net response could not be decoded");
} else {
Log.d("ContentValues", "Unknown Error while fetching safety net results: " + e.getMessage());
}
}
});
}
None of the handlers gets a response even after waiting for as long as 30 seconds. Can someone please help out.