1

In my application, I want to start a service when user is connected with the internet using manifest declaration but in android documentation connectivity broadcast can only be registered inside a component. For that app should be running state. But I want to start the service to show notification independent of app state (running, paused, destroyed etc).
Simply I want to implement Gmail notification functionality in my app like Gmail shows notification when user is connected to internet independent of Gmail app state.Thanks!!

sak
  • 1,230
  • 18
  • 37
Shiv
  • 122
  • 2
  • 16
  • here is complete solution have look [this](https://stackoverflow.com/q/22310691/5110595) – Hemant Parmar Jan 04 '18 at 12:24
  • Most likely, Gmail is using FCM (or something derived from that), not some service that runs in response to Internet connectivity. – CommonsWare Jan 04 '18 at 12:26
  • @CommonsWare I alsowant to use FCM but to convert fcm message in notification we need to build notification in some service right – Shiv Jan 04 '18 at 12:28
  • You can raise a `Notification` from any `Context`. Gmail probably does this from [a service started by FCM](https://firebase.google.com/docs/cloud-messaging/android/receive), as Gmail has other work to do that takes time (e.g., downloading the email messages and writing them to disk). – CommonsWare Jan 04 '18 at 12:31
  • @CommonsWare Actually i want to listen for childAdded() callback from real time database provided by firebase in background. – Shiv Jan 04 '18 at 12:34

1 Answers1

1

For anything you would want your application to do when it's not running, it has to be in a background service. You can implement a continuous check for the internet connectivity in that service.

Extend FirebaseMessagingService with your custom logic to listen to any FCM notification. https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService

Vishal Raja
  • 303
  • 1
  • 8
  • I want to use FCM service. – Shiv Jan 04 '18 at 12:37
  • Extend FirebaseMessagingService with your custom logic to listen to any FCM notification. This service would continue to run even when your application is closed and would react to any FCM notification according to your custom logic. – Vishal Raja Jan 04 '18 at 13:00
  • And if I have to listen on real time database change..then – Shiv Jan 04 '18 at 13:04
  • So we have to start activity in some component right? So for this that component should be in running state – Shiv Jan 04 '18 at 13:13
  • If you're talking about the database changes on your server, you would need to identify those changes and send FCM notification to your application from your backend about those changes. – Vishal Raja Jan 04 '18 at 13:24
  • The component should run once to kick-start this background service and then can be closed. Your custom background service would keep running. – Vishal Raja Jan 04 '18 at 13:26
  • Yeah but without using any component how can I start service. Is there any way. – Shiv Jan 04 '18 at 13:39
  • 1
    No. This is your application-specific service and application should run at least once to kick-start this service. At best, You can have this service started in the first application activity itself. No other way. – Vishal Raja Jan 04 '18 at 13:41