I need to make background process in android studio, that will work when phone is turned off and will listen to the firebase realtime database and send notifications if the user data matches sent from another user firebase data.
I have a realtime database in which, when the "SOS" button is pressed, data is sent from which user the message goes and to whom. That's how it looks like in Firebase console:
Firebase database {
Sos(all sos button clicks){
RandomID( like -N3AB5hwgkPO5kCqdaJf){
emailfrom(who clicked button)
emailto(user who will receive message)
time
}
}
}
i need to scan section "sos" all the time, and if emailto matches with my email, app will send notification. I made the prototype with "extends service", but it doesn't even work, and does not perform any function.
public class SosListener extends Service {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String email = user.getEmail();
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
DatabaseReference dataBase = FirebaseDatabase.getInstance().getReference();
dataBase.child("Sos/").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot snapshot, String previousChildName) {
String emailTo = snapshot.child("emailTo").getValue(String.class);
if (emailTo == email) {
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot snapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
Here is also MainActivity where I start this Service:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, SosListener.class));
}
}
How can I make it?