I am using geofence service in my app and using exactly the same sample as given in the google documentation.
Doing some research i found that the Intent service is killed when the app is killed. I want to receive the geofence notifications even when the app is not running. How can i do that.?
I have looked for the examples using broadcast receivers but they are old and use classes which are deprecated.
Related
With the transition to android 8 I encountered an issue that geofences in app are not working anymore when app is killed.
I implemented geofences as per android developers guide, so no reason to show any code snipplets.
With some small modifications and explicit broadcast receiver I was able to receive geofence notification when app was running and when app was in background.
Still no luck with case when app is killed. If anyone had made transition to android 8 and has working geofences please share your experience.
You will have to change the PendingIntent that the geofence triggers to a BroadcastReceiver instead of a Service. The system will no longer let a Service start when your app is in the background.
Also noted in this answer.
This only happens on Android Oreo. I'm using Play Services 11.4.2.
I'm registering geofences using the GeofencingClient and the addGeofences method with a pendingIntent to an IntentService that is handling geofence transitions.
It looks like the intent sent by the play services is blocked by the OS in certain conditions.
The system logs the following:
Background start not allowed: service Intent { cmp=my.app.id/my.package.struct.GeofenceTransIntentService (has extras) } to my.app.id/my.package.struct.GeofenceTransIntentService from pid=-1 uid=10154 pkg=my.app.id
as soon as I add a geofence in the following situations:
when I add it after the device boots
when I add it after the app was swiped
In both occasions the app is actually already running in the background (since I'm able to run the code that adds the geofence) because I listen to the PROVIDERS_CHANGED, BOOT_COMPLETED.
This is caused by the new Android Oreo background service limitations.
You have to change the PendingIntent from using a Service to using a BroadcastReceiver.
See this CodeLab for further information and example code.
In addition to switching to a BroadcastReceiver as mentioned in the accepted answer, I'd like to point out that although you will still receive BOOT_COMPLETED, you will not be able to receive PROVIDERS_CHANGED any longer.
PROVIDERS_CHANGED is an implicit broadcast and is not in the list of broadcast exceptions.
BOOT_COMPLETED is in the list of broadcast exceptions which is why you'll still receive it.
If you want to learn about an alternative approach to get the hook when location services is toggled, please see my answer to a related question for more details.
I wrote an app which implemented the FirebaseMessagingService; I killed the process which the service belongs to, but the app can still receive the notification from firebase console, except that I forced to stop the app.
So, I just want to know why the app can still receive the notification event though the service not alive? What's the mechanism of android?
I don't have experience in Firebase, but in previous Google Cloud Messaging it was working this way: at first notifications (pushes) are received by Google Play Services and then redirected to your app starting proper broadcast receiver or service. probalby Firebase is working same way - it's starting new service when it is not running currently
I have looked on similar threads however couldn't find a definitive answer.
For android 3.1+, if an app is force killed it doesn't receive broadcasts.
Force killed stops all running services and proccesses.
Does this mean if my app doesn't have a running service and is swiped out of recent apps then it will not receive GCM notifications?
Or does this only apply to when the force stop button is actually pressed.
Maybe you know the (deprecated) version of GCM. There we had to implement a WakefulBroadcastReceiver service which was started automatically when reveicing any GCM notification. This service had to "wake up" your app and in turn is able to start any of your own services.
This of course is still valid for the most recent GCM API-version but there you have to extend GcmListenerService which also is called by a WakefulBroadcastReceiver.
For detailed information on how to implement this please refer to Google's code sample.
To say it short - yes it will work if it is implemented correctly.
I hope that we cannot receive any messages ,when we force stop the default messenger app in our android device.I force stop default messenger app and sent message to that android device. I get message to that device.why this device receive message?
On Android 3.1+, a BroadcastReceiver will only work after its app was force-stopped if an explicit Intent is used to send it a broadcast. Here, by "explicit Intent", I mean one that contains the ComponentName of the receiver, in addition to possibly other data like an action string.
It is unclear what you mean by "the default messenger app" and "messages". If you mean SMS, I would expect that on Android 4.4+, if the user's default SMS client is force-stopped, it would still get SMS messages, as Android probably uses an explicit Intent to talk to it. However, on Android 4.3 and below, I would not expect a force-stopped SMS client to receive messages, as SMS_RECEIVED was just an ordinary ordered broadcast then.
This happens often because there is a service that is listening. The app uses the service to connect with Google Cloud Messaging and receive the messages. You'll need to disable the service. You can view Settings -> Apps, then swipe left to view All or Running and view the services.
Extending a bit further Steve's answer, this is probably an exemplary case that describes why programmers must control any Services in their applications. Many people just declare and run them, and then they forget about them. Services run in a different component than the foreground app, so if you 'close' your app but you don't stop the Service, it will be still running and that's probably what you're experiencing. However, many people tend to misunderstand this concept. This is a different component, but not a different thread nor a different process.
As per the official Android Developer Guide:
A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. [...]
You don't specify what kind of message system uses your app, but if it is GCM (Google Cloud Messaging) or some alike system, it indeed uses a background Service to receive notifications from the central server and you're getting them because your foreground app is closed but your Service is still running.
To stop a Service, you'll need to call Context's stopService() method, as defined in the documentation prior to stopping your foreground application.