How to listen for firebase events in android support?

Tried to listen for firebase events in the background. Please find the code snippet below,

@Override public int onStartCommand(Intent intent, int flags, int startId) { ValueEventListener postListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Post post = dataSnapshot.getValue(Post.class); } @Override public void onCancelled(DatabaseError databaseError) { } }; mPostReference.addValueEventListener(postListener); return START_STICKY; } 

The onDataChange () method becomes until my activity is active, as soon as the Activity exits, the method is not called.

Someone please help.

+5
source share
1 answer

In your manifest, make sure android:stopWithTask not set to true . Either set it to false , or leave it completely, because by default it is false . Thus, the Service will not be connected with the life cycle of the activity. Your manifest should look something like this:

  <service android:name=".your_package_here.your_service_class_here" android:enabled="true" android:stopWithTask="false"/> 
0
source

Source: https://habr.com/ru/post/1265077/


All Articles