Monday 12 April 2021

Firebase : -Firestore Live Location

FIRESTORE Live Location 

AUTHENTICATION:-






EDIT RULES :-

rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /dev/{userId} {
allow read, update, delete: if request.auth.uid == userId;
allow create: if request.auth.uid != null;
match /LiveData/{userId} {
allow read, update, delete,create: if request.auth.uid != null;

}
}
}

}

ANDROID CODE:

String token;
FirebaseAuth authref;
private FirebaseFirestore db;
<string name="FIREBASE_DATABASE_NAME">dev</string>
<string name="FIREBASE_DATABASE_NODE_NAME">LiveData</string>
<string name="FIREBASE_EMAIL_DB">amitrawat@gmail.com</string>
<string name="FIREBASE_PASS_DB">123456789</string>
@Override
public void onCreate() {
super.onCreate();

try {
        setup();
notificationToken();
loginFirebase();
} catch (Exception e) {
e.printStackTrace();

}


}
public void setup() {

db = FirebaseFirestore.getInstance();

}
private void notificationToken() throws Exception {
try {
FirebaseInstanceId.getInstance().getInstanceId()
.addOnCompleteListener(new OnCompleteListener<InstanceIdResult>() {
@Override
public void onComplete(@NonNull Task<InstanceIdResult> task) {
if (!task.isSuccessful()) {
return;
}
token = task.getResult().getToken();

Log.e("token", token);
}
});
} catch (Exception e) {
throw new Exception(e);
}
}


  private void loginToFirebase() {
//Authenticate with Firebase, using the email and password we created earlier//
String email = getString(R.string.FIREBASE_EMAIL_DB);
String password = getString(R.string.FIREBASE_PASS_DB);
//Call OnCompleteListener if the user is signed in successfully//
authref = FirebaseAuth.getInstance();
authref.signInWithEmailAndPassword(
email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(Task<AuthResult> task) {
if (task.isSuccessful()) {
try {
requestLocationUpdates();
} catch (Exception e) {

}

} else {
Log.d(TAG, "Firebase authentication failed");
}
}
});
}


 private void requestLocationUpdates() {

LocationRequest request = new LocationRequest();
request.setInterval(60000);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
FusedLocationProviderClient client = LocationServices.getFusedLocationProviderClient(this);
int permission = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION);
//If the app currently has access to the location permission...//
if (permission == PackageManager.PERMISSION_GRANTED) {
//...then request location updates//
client.requestLocationUpdates(request, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
//Get a reference to the database, so your app can perform read and write operations//
if (isNullOrEmpty(token)) {
try {
notificationToken();
} catch (Exception e) {
e.printStackTrace();
Util.Crash(e);
}
} else {
try {

Location location = locationResult.getLastLocation();
if (location != null) {
FirebaseDTO locdto = new FirebaseDTO ();
locdto.setLatitude(location.getLatitude());
locdto.setLongitude(location.getLongitude());

locdto.setLastUpdatedOn(String.valueOf(new Timestamp(System.currentTimeMillis())));
Log.e("loc1", String.valueOf(location.getLatitude()));
Log.e("loc2", String.valueOf(location.getLongitude()));


// Add a new document with a generated ID\

db.collection(getResources().getString(R.string.FIREBASE_DATABASE_NAME)).
document(authref.getUid()).
collection(getResources().getString(R.string.FIREBASE_DATABASE_NODE_NAME)).
document(token).
set(locdto)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "DocumentSnapshot successfully written!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error writing document", e);
}
});

}


} catch (Exception e) {
Util.Crash(e);
}

}

}
}, null);
}
}
public class FirebaseDTO {
private double latitude;
private double longitude
private String lastUpdatedOn;

}

 

1 comment:

  1. Howdy! I simply would like to give an enormous thumbs up for the nice information you might have right here on this post. I can be coming again to your blog for more soon.macbook air reparatur berlin

    ReplyDelete