fix: pass foreground service type to startForeground on Android 14+

On Android 14+ (API 34), when a service declares
foregroundServiceType="specialUse" in the manifest it must also pass
FOREGROUND_SERVICE_TYPE_SPECIAL_USE to startForeground() at runtime.
Without this the system rejects the notification with
CannotPostForegroundServiceNotificationException ("Bad notification for
startForeground"), which crashes the service on affected devices such as
the Motorola moto g15 running Android 15.

Use ServiceCompat.startForeground() (from androidx.core) so the type is
forwarded only on API 29+ where the three-argument overload exists, and
FOREGROUND_SERVICE_TYPE_SPECIAL_USE is gated on API 34+.

https://claude.ai/code/session_014KKpPSKZSnFrZf6tmMdCiz
This commit is contained in:
Claude
2026-03-18 09:23:02 +00:00
parent bb0dcfa0bb
commit a56bb15767
@@ -2,11 +2,14 @@ package com.greenart7c3.nostrsigner.service
import android.app.Service
import android.content.Intent
import android.content.pm.ServiceInfo
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.ServiceCompat
import com.greenart7c3.nostrsigner.Amber
import com.greenart7c3.nostrsigner.BuildFlavorChecker
import com.greenart7c3.nostrsigner.LocalPreferences
@@ -169,7 +172,18 @@ class ConnectivityService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d(Amber.TAG, "onStartCommand")
startForeground(1, Amber.instance.stats.createForegroundNotification())
val foregroundServiceType =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_SPECIAL_USE
} else {
0
}
ServiceCompat.startForeground(
this,
1,
Amber.instance.stats.createForegroundNotification(),
foregroundServiceType,
)
return START_STICKY
}
}