change SendStatusService type to shortService (#4292)

This way the `FOREGROUND_SERVICE_REMOTE_MESSAGING` permission is not
needed and we should be able to publish on Google Play again. Drawback:
The service can get killed after a while (usually 3 mins) on Android 14.
I also tried using [user initiated data transfer
jobs](https://developer.android.com/about/versions/14/changes/user-initiated-data-transfers),
but that is not available on all api levels, and `WorkManager`, but that
is a huge refactoring and sending would probably work differently than
before.
This commit is contained in:
Konrad Pozniak 2024-02-29 12:21:15 +01:00 committed by GitHub
parent 1fab0b8460
commit 7448fd2416
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 6 deletions

View File

@ -8,7 +8,6 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>
<uses-permission android:name="android.permission.VIBRATE" /> <!-- For notifications -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING" />
<application
android:name=".TuskyApplication"
@ -197,7 +196,7 @@
</service>
<service android:name=".service.SendStatusService"
android:foregroundServiceType="remoteMessaging"
android:foregroundServiceType="shortService"
android:exported="false" />
<provider

View File

@ -141,15 +141,23 @@ class SendStatusService : Service(), Injectable {
statusesToSend[sendingNotificationId] = statusToSend
sendStatus(sendingNotificationId--)
} else {
if (intent.hasExtra(KEY_CANCEL)) {
cancelSending(intent.getIntExtra(KEY_CANCEL, 0))
}
} else if (intent.hasExtra(KEY_CANCEL)) {
cancelSending(intent.getIntExtra(KEY_CANCEL, 0))
}
return START_NOT_STICKY
}
override fun onTimeout(startId: Int) {
// https://developer.android.com/about/versions/14/changes/fgs-types-required#short-service
// max time for short service reached on Android 14+, stop sending
statusesToSend.forEach { (statusId, _) ->
serviceScope.launch {
failSending(statusId)
}
}
}
private fun sendStatus(statusId: Int) {
// when statusToSend == null, sending has been canceled
val statusToSend = statusesToSend[statusId] ?: return