Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Started service vs bound service #559

Open
fabmazz opened this issue Feb 15, 2024 · 10 comments
Open

Started service vs bound service #559

fabmazz opened this issue Feb 15, 2024 · 10 comments

Comments

@fabmazz
Copy link
Contributor

fabmazz commented Feb 15, 2024

First of all, thanks for the library, it's quite good.

However, I'm using this library since a few months, and it has caused me some headaches with the newest version of Android 14. This is because of the issue of foreground services.

I've looked at the code of the library, and it seems you start the service manually, putting it in on foreground when there is a notification, however it's not clear to me why, as you immediately bind the client to the service. From the documentation on bound services, it seems that calling bindService is enough on its own to start the service.

I'm only asking because for my app, I just need a background way to connect to an MQTT server, and only download data when the user is using the app. Maybe using a foreground service is too much, and by removing the need to call startService or startForeground, all the library could be simplified.

@fabmazz
Copy link
Contributor Author

fabmazz commented Feb 15, 2024

P.S. From https://developer.android.com/develop/background-work/services/fg-service-types#data-sync, it seems that Google intends to remove (sooner or later, we'll never know) the data-sync type, which the library currently uses.

@hannesa2
Copy link
Owner

If you see some improvements, I'm happy to merge it

@fabmazz
Copy link
Contributor Author

fabmazz commented Feb 16, 2024

Well I was just wondering the rationale. I could test the library without the started service on my app, but that's just one use case.

Just so you know, atm after review a bit the code of the library, I've decided not to use the Foreground Service option, since it works well all the same, and I'm removing the authorization for the FOREGROUND_SERVICE_DATA_SYNC in the app manifest.

@hannesa2
Copy link
Owner

With 4.x you don't need any foreground-something as well

@craigt543nz
Copy link

The library is great. Thanks for your effort. I've had the same issue above and Google has rejected my app for production because of the FOREGROUND_SERVICE_DATA_SYNC permission request. I've altered my manifest to remove the permission and everything seems to run happily through Internal Testing on Google Play Console. Hopefully it will get through to production.

@hannesa2
Copy link
Owner

hannesa2 commented Mar 7, 2024

I added this

<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
because api34 showed me this

image You can see it, when you check out de78477

When you have a better way to solve it, please make a pull request

@craigt543nz
Copy link

Thanks Hannes. Yes, I agree that the permission in the manifest is required to use startForeground(). The problem is that our friends at Google have recently changed what is allowed when using startForeground. The developer has to specifically request permission from Google and include a video showing their use case. This is what Google say:

With the exceptions of systemExempted and shortService foreground service types, apps are only allowed to declare a foreground service permission if the use:

  • Provides a feature that is beneficial to the user and relevant to the core functionality of the app.
  • Is initiated by the user or is user perceptible (for example, audio from playing a song, cast media to another device, accurate and clear user notification, user request to upload a photo to the cloud).
  • Can be terminated or stopped by the user.
  • Can’t be interrupted or deferred by the system without causing a negative user experience or causing the user anticipated feature to not work as intended (for example, a phone call needs to start immediately and can’t be deferred by the system).
  • Runs only for as long as necessary to complete the task.

Take a look at: https://notifications.google.com/g/p/ANiao5o1OhhH3puC6LdbOSDftV-Wb6IKAYqjJvrVccXzKuLmw9kkq8k3H7yG6CFaasHVxR8dFTvHi-gb6b4SGGQqfL4QEuk2wgsl2LD3UaK-iHAj5pTS1P6CmoFTKavgYdlsNTZTx2mLNoGT3KLIFyqR8XQM--L3rPgsWKe35HbOB4qnhR6cn7cw-LsuvjPJzvsjGg2yH3c6zEFON2sixGkMVp7Y94Bn2L7LFsH4IDQBhTp6VjrHrVtQV9h9S-ewv5n1Y5jdtYf_VH3JOesHwT7W6iI0U8muDmBotWCpaHICzBdB0Y5i

The startForeground function in the library is provided to allow mqtt to continue to run when the app is in the background. I'm not using this feature, so I can get away with removing the permission.

From what Google requires (see above), I think it will be challenging to get approval from Google to run mqtt in the background. I don't know what the use cases might be for running mqtt when the app is in the background, but if there aren't any, then it would be simple to remove this functionality. It would be nice to hear from anyone who needs to run mqtt in the background and get their thoughts.

@hannesa2
Copy link
Owner

hannesa2 commented Mar 8, 2024

Thank you for the explanation, and what do you recommend now ?

@fabmazz
Copy link
Contributor Author

fabmazz commented Mar 8, 2024

I had the same problem @craigt543nz and since I didn't need the MQTT service active when the app was off screen, I just removed the permission in the manifest and didn't put a startForeground flag in the MQTT Library.

My two cents here is that it should be up to the app developer to decide whether to use it in Foreground or not, and add the permission to the app manifest accordingly. Thus, I think that the solution would be to remove the permission from the library manifest (if that's possible).

@craigt543nz
Copy link

Yes, I agree with you @fabmazz, removing the permission from the library manifest is a better solution. And add a note to the Readme that if you want to use startForeground (i.e. the setForegroundService function from the class MqttAndroidClient), the developer will need to add the permissions to their manifest and if they want to publish on the Play Store they will need to fulfill Google's requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants