Skip to content
Snippets Groups Projects
Commit 7ce2e5e8 authored by Emirhan Gürkan's avatar Emirhan Gürkan
Browse files

Merge branch 'Android-System-Specifics' into 'main'

add request post notification permission

See merge request !1
parents 1a2984d8 3203111b
No related branches found
No related tags found
1 merge request!1add request post notification permission
......@@ -3,10 +3,20 @@
<component name="deploymentTargetDropDown">
<value>
<entry key="MainActivity">
<State />
</entry>
<entry key="TimerScreenPreview">
<State />
<State>
<targetSelectedWithDropDown>
<Target>
<type value="QUICK_BOOT_TARGET" />
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="C:\Users\emirg\.android\avd\Medium_Phone_API_34.avd" />
</Key>
</deviceKey>
</Target>
</targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2024-08-28T18:45:26.330794200Z" />
</State>
</entry>
<entry key="app">
<State />
......
......@@ -2,7 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:name=".MyAppChannel"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
......
......@@ -11,22 +11,59 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.example.myapplication.UI_Components.main_app
import com.example.myapplication.ui.theme.MyApplicationTheme
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Box
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import androidx.core.content.ContextCompat
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier
.fillMaxSize()
.padding(10.dp),
color = MaterialTheme.colorScheme.background
) {
val context = LocalContext.current
var hasPermission by remember { mutableStateOf(hasNotificationPermission(context)) }
val permissionLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestPermission(),
onResult = { isGranted -> hasPermission = isGranted }
)
LaunchedEffect(key1 = hasPermission) {
if (!hasPermission && Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
permissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}
main_app()
}
}
}
}
private fun hasNotificationPermission(context: Context): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
return ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED
}
return true
}
}
\ No newline at end of file
package com.example.myapplication
import android.app.Application
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
class MyAppChannel: Application() {
override fun onCreate() {
super.onCreate()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val channel = NotificationChannel(
"channel_id", "myChannel", NotificationManager.IMPORTANCE_DEFAULT
)
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.createNotificationChannel(channel)
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment