I am using Firebase for creating a small chat application. I want ChildEventListener
to keep listening to the firebase database location though my app is in bac
Try firebase cloud messaging with firebase functions.
Use service to listen your ChildEventListener
public class ChildEventListener extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Adding a childevent listener to firebase
Firebase myFirebaseRef = new Firebase("FirebaseURL");
myFirebaseRef.child("FIREBASE_LOCATION").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
//Do something using DataSnapshot say call Notification
}
@Override
public void onCancelled(FirebaseError error) {
Log.e("The read failed: ", error.getMessage());
}
});
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Log.e("The read failed: ", firebaseError.getMessage());
}
});
return START_STICKY;
}
}
register your Service inside Manifest
<service android:name=".ChildEventListener "/>
Start your Service and listen for childEvents, where/when to start your service is up to your chat app design