1

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?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You need to add the service in the manifest not in the activity – Sambhav Khandelwal May 29 '22 at 15:45
  • @Sambhav.k yes, i added it in manifest, i need help with the code. – asaaasssOAS May 29 '22 at 15:54
  • you can't make services without notifications. You need to add a notification of the service will stop in 10 seconds – Sambhav Khandelwal May 29 '22 at 16:01
  • @Sambhav.k oh, and how can i read data from firebase constantly and if app is turned off? – asaaasssOAS May 29 '22 at 16:06
  • Does this answer your question? [android-firebase childEventListener in service](https://stackoverflow.com/questions/46618432/android-firebase-childeventlistener-in-service) – Sambhav Khandelwal May 29 '22 at 16:27
  • 50/50. i dont know how to write path to emailto field. because every id for sos clicks is random. – asaaasssOAS May 29 '22 at 16:36
  • So, whenever a new sos is added, you want to check if the `mailto` is my email(from auth) and then do something if that is the case? Am I right? – Sambhav Khandelwal May 29 '22 at 16:49
  • @Sambhav.k yes, you are right. – asaaasssOAS May 29 '22 at 16:52
  • so, what is not working in your code? Did you they that link I had provided – Sambhav Khandelwal May 29 '22 at 16:52
  • yes, that maybe works, but i dont know, how to write path to emailto field in get reference(). Sos{randomid{emailfrom, emailto, time}} – asaaasssOAS May 29 '22 at 16:55
  • you need not do that. you are getting it in the `onChildAdded` right? Then, what goes wrong. Can you share the image of the structure of your database? – Sambhav Khandelwal May 29 '22 at 16:56
  • also, first try to debug your app or add log statements that you have come in `onChildAdded` and then printing the name and other info. That will help u know what is going on in your app – Sambhav Khandelwal May 29 '22 at 16:57
  • i cant share pictures because i dont have 15 rep. but its like Sos->RandomMsgId that firebase generates->emailfrom emailto and time fields @Sambhav.k i need to get emailto from randomid sector – asaaasssOAS May 29 '22 at 16:58
  • Hmmmmm. Ok. For that, you can refer to [this](https://github.com/sambhav2358/Backgroud-Stopwatch-Android/blob/main/app/src/main/java/com/sambhav2358/testing/TimerService.java) service class. And, when you compare your code, you will find some differences. And, one of the biggest difference is that you are writing the entire code in `onCreate` which is wrong. Instead you should write that code in `onStartCommand`. If u r having trouble understanding this, I can also post it as an answer – Sambhav Khandelwal May 30 '22 at 04:40
  • First of all, stop ignoring errors. At a minimum, please add `Log.d(TAG, error.getMessage());`. Do you get something printed out in the logcat? – Alex Mamo May 30 '22 at 09:53

0 Answers0