The method is to create a user service https://developer.android.com/reference/android/app/Service.html that keeps a connection through a socket or polls some remote server at intervals.
But I believe that the Firebase service is more advanced and optimized for battery consumption and, in particular, when using sleep mode. It is not trivial to make a service that will have a fast response and CPU consumption. I think that the development of user services will lead to native (C ++) coding of network routine libraries.
Also, be aware that Firebase provides its own authentication service with the ability to seamlessly convert your anonymous user into a registered user. There are many advantages to Firebase services. And cons: your users must install Google Play Services, if your application grows, you will pay Firebase.
See here Android push notifications without Google service
source share