Firebase: keep listening to ChildEventListener though app exits

后端 未结 2 969
余生分开走
余生分开走 2021-01-02 06:49

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

相关标签:
2条回答
  • 2021-01-02 07:40

    Try firebase cloud messaging with firebase functions.

    0 讨论(0)
  • 2021-01-02 07:51

    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

    0 讨论(0)
提交回复
热议问题