From 3203111bc96e9d6e634e1a33040f5d779dbf9049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emirhan=20G=C3=BCrkan?= <emirhan.guerkan@smail.th-koeln.de> Date: Wed, 28 Aug 2024 20:50:56 +0200 Subject: [PATCH] add request post notification permission --- .idea/deploymentTargetDropDown.xml | 18 +++++++-- app/src/main/AndroidManifest.xml | 3 ++ .../com/example/myapplication/MainActivity.kt | 39 ++++++++++++++++++- .../com/example/myapplication/MyAppChannel.kt | 22 +++++++++++ 4 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/com/example/myapplication/MyAppChannel.kt diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index b7ae789..568909b 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -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 /> diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e013846..85192d5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -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" diff --git a/app/src/main/java/com/example/myapplication/MainActivity.kt b/app/src/main/java/com/example/myapplication/MainActivity.kt index 18497d7..21bf3dd 100644 --- a/app/src/main/java/com/example/myapplication/MainActivity.kt +++ b/app/src/main/java/com/example/myapplication/MainActivity.kt @@ -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 diff --git a/app/src/main/java/com/example/myapplication/MyAppChannel.kt b/app/src/main/java/com/example/myapplication/MyAppChannel.kt new file mode 100644 index 0000000..d58527a --- /dev/null +++ b/app/src/main/java/com/example/myapplication/MyAppChannel.kt @@ -0,0 +1,22 @@ +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 -- GitLab