Niv Shalom
Niv Shalom

Reputation: 15

Android Service Using Firebase Not Working Properly

I am building an app with chatting features. I am using Firebase as the database and I am trying to add a service that notify you when you get a message when you are outside the app and the stops itself once you have been notified once per user you chat with but for some reason. When I run the service it runs one time then stops itself and does not wait for notifications anyone knows why? the code in question

public class Pmessginfnotifiysvc extends Service {
   Thread thread;
   FirebaseAuth auth12;
   Boolean first;
   long stop;
   DatabaseReference databaseReference;
   ValueEventListener valueEventListener;
   @Nullable
   @Override
   public IBinder onBind(Intent intent) {
      return null;
   }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
       thread = new Thread(new msgtracker());
       thread.setPriority(Thread.MAX_PRIORITY);
       thread.start();
       return START_STICKY;
   }
   private class msgtracker implements Runnable{

       public msgtracker(){}

       @Override
       public void run() {
           if (!thread.isInterrupted()){
               auth12 = FirebaseAuth.getInstance();
               databaseReference = FirebaseDatabase.getInstance().getReference().child("Users").child(auth12.getCurrentUser().getUid());
            databaseReference.child("PChat").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    first = true;
                    stop = dataSnapshot.getChildrenCount();
                    for (final DataSnapshot kid:dataSnapshot.getChildren()) {
                        final DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("Chat").child(kid.getValue().toString());
                        valueEventListener = new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                if (!first){
                                    MyNotificationManager.getInstance(getApplicationContext()).displayNotification("TvTime",kid.getKey() + " Has Sent You A Message!");
                                    db.removeEventListener(valueEventListener);
                                    stop--;
                                }
                                else {
                                    first = false;
                                }
                                if (stop == 0){
                                    thread.interrupt();
                                    stopSelf();
                                }
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        };
                        first = true;
                        db.addValueEventListener(valueEventListener);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    }
}

}

Upvotes: 1

Views: 326

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317828

Android may stop your service if the app is not in the foreground. It does this to save resources when the app isn't being used. It also might stop your app from doing any networking, or even kill the app process completely. There's nothing you can do to prevent this, other than making it a foreground service. A foreground service is probably not the best thing to do for your case, nor is it the best thing for your users.

Read more about limitations on background services.

It's better to use Firebase Cloud Messaging to notify your app when something has changed that it might be interested in.

Upvotes: 1

Related Questions