Zeige Notification

This commit is contained in:
2026-01-08 11:13:05 +01:00
parent e2e87b90f0
commit b75420415b
4 changed files with 128 additions and 3 deletions

View File

@@ -3,18 +3,25 @@ package de.jadehs.strassenschadenpro2
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.os.Looper
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationResult
import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.Priority
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONObject
import java.net.URL
class LocationService: Service() {
@@ -36,12 +43,46 @@ class LocationService: Service() {
for (location in locationResult.locations){
val latitude = location.latitude
val longitude = location.longitude
val sharedPreferences=getSharedPreferences("settings", Context.MODE_PRIVATE)
val radius= sharedPreferences.getInt("radius", 50)
Log.e("LocationService", "Location: $latitude, $longitude")
CoroutineScope(Dispatchers.IO).launch {
val urlString = "https://services9.arcgis.com/UVxdrlZq3S3gqt7w/ArcGIS/rest/services/StrassenSchaeden/FeatureServer/0/query?where=1%3D1&objectIds=&geometry=$longitude%2C$latitude&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelIntersects&resultType=none&distance=$radius&units=esriSRUnit_Meter&outDistance=&relationParam=&returnGeodetic=false&outFields=&returnGeometry=true&featureEncoding=esriDefault&multipatchOption=xyFootprint&maxAllowableOffset=&geometryPrecision=&outSR=&defaultSR=&datumTransformation=&applyVCSProjection=false&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnQueryGeometry=false&returnDistinctValues=false&cacheHint=false&collation=&orderByFields=&groupByFieldsForStatistics=&returnAggIds=false&outStatistics=&having=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&returnTrueCurves=false&returnExceededLimitFeatures=true&quantizationParameters=&sqlFormat=none&f=pgeojson&token="
val jsonString = URL(urlString).readText()
val jsonObject = JSONObject(jsonString)
val features = jsonObject.getJSONArray("features")
val anzahl = features.length()
if (anzahl > 0){
showNotification(this@LocationService, anzahl)
}
}
}
}
}
}
fun showNotification(context: Context, anzahl: Int){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val serviceChannel = NotificationChannel(
channelID,
"Location Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
val manager = getSystemService(NotificationManager::class.java)
manager?.createNotificationChannel(serviceChannel)
val notification = NotificationCompat.Builder(context, channelID)
.setContentTitle("Straßenschaden gefunden!")
.setContentText("$anzahl Strassenschäden gefunden!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSmallIcon(R.drawable.outline_adb_24)
.build()
with(NotificationManagerCompat.from(context)){
notify(notificationID, notification)
}
}
}
override fun onStartCommand(intent: Intent?,flags: Int,startId: Int): Int {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val serviceChannel = NotificationChannel(

View File

@@ -87,6 +87,6 @@ fun ContentScreen(
0 -> MapPage(modifier = modifier, mapViewModel = mapViewModel,selectedObjectId=selectedObjectId, onObjectIdUsed = onObjectIdUsed)
1 -> CreatePage(modifier = modifier, mapViewModel = mapViewModel)
2 -> ListPage(modifier = modifier, listViewModel = listViewModel, onFeatureClick = onFeatureSelected)
3 -> SettingsPage()
3 -> SettingsPage(modifier = modifier)
}
}

View File

@@ -1,10 +1,89 @@
package de.jadehs.strassenschadenpro2.pages
import android.content.Context
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Slider
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
@Composable
fun SettingsPage(modifier: Modifier = Modifier) {
Text("Einstellungen")
val context = LocalContext.current
val sharedPreferences = remember { context.getSharedPreferences("settings", Context.MODE_PRIVATE) }
var radius by remember {
mutableFloatStateOf(sharedPreferences.getInt("radius", 50).toFloat())
}
Column(modifier = modifier.padding(16.dp)) {
Text("Einstellungen", style = MaterialTheme.typography.headlineMedium)
Text("Suchradius (in Meter)",modifier = Modifier.padding(top=16.dp))
OutlinedTextField(
value = radius.toInt().toString(),
modifier = Modifier.padding(top=8.dp),
label = { Text("Radius") },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
onValueChange = { newValue ->
var textValue = newValue
textValue = textValue.replace(" ","")
textValue = textValue.filter { it.isDigit() }
val parsed = newValue.toIntOrNull()
if (parsed != null) {
val clamped = parsed.coerceIn(20, 500)
radius = clamped.toFloat()
sharedPreferences.edit().putInt("radius", clamped).apply()
}
},
singleLine = true
)
Slider(
value = radius,
onValueChange = { newValue ->
radius = newValue
sharedPreferences.edit().putInt("radius", newValue.toInt()).apply()
},
valueRange = 20f..500f,
modifier = Modifier.padding(top=8.dp)
)
}
}

View File

@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="960" android:viewportWidth="960" android:width="24dp">
<path android:fillColor="@android:color/white" android:pathData="M200,440L200,400Q200,328 232.5,268.5Q265,209 320,171L245,96L280,60L365,145Q391,133 420.5,126.5Q450,120 480,120Q510,120 539.5,126.5Q569,133 595,145L680,60L715,96L640,171Q695,209 727.5,268.5Q760,328 760,400L760,440L200,440ZM600,360Q617,360 628.5,348.5Q640,337 640,320Q640,303 628.5,291.5Q617,280 600,280Q583,280 571.5,291.5Q560,303 560,320Q560,337 571.5,348.5Q583,360 600,360ZM360,360Q377,360 388.5,348.5Q400,337 400,320Q400,303 388.5,291.5Q377,280 360,280Q343,280 331.5,291.5Q320,303 320,320Q320,337 331.5,348.5Q343,360 360,360ZM480,920Q363,920 281.5,838.5Q200,757 200,640L200,480L760,480L760,640Q760,757 678.5,838.5Q597,920 480,920Z"/>
</vector>