Skip to content

Commit

Permalink
Attempt to request notification permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
codeguru42 committed Aug 8, 2023
1 parent 7bfaae9 commit e498249
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
52 changes: 36 additions & 16 deletions app/src/main/java/codeguru/gocapture/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
package codeguru.gocapture

import android.Manifest
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Image
import androidx.compose.material.icons.filled.PhotoCamera
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.core.content.FileProvider
import androidx.navigation.NavController
import androidx.core.content.ContextCompat
import androidx.navigation.compose.rememberNavController
import codeguru.gocapture.ui.theme.GoCaptureTheme
import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.messaging.FirebaseMessaging
import java.io.File

class MainActivity : AppCompatActivity() {
private val TAG: String = javaClass.name
private val CHANNEL_ID: String = "Go Capture"

private val requestPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission(),
) { isGranted: Boolean ->
if (isGranted) {
// FCM SDK (and your app) can post notifications.
} else {
// TODO: Inform user that that your app will not show notifications.
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Expand Down Expand Up @@ -81,6 +78,29 @@ class MainActivity : AppCompatActivity() {
notificationManager.createNotificationChannel(channel)
}
}

fun requestNotificationPermission() {
// This is only necessary for API level >= 33 (TIRAMISU)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (
ContextCompat.checkSelfPermission(
this,
Manifest.permission.POST_NOTIFICATIONS
) ==
PackageManager.PERMISSION_GRANTED
) {
// FCM SDK (and your app) can post notifications.
} else if (shouldShowRequestPermissionRationale(Manifest.permission.POST_NOTIFICATIONS)) {
// TODO: display an educational UI explaining to the user the features that will be enabled
// by them granting the POST_NOTIFICATION permission. This UI should provide the user
// "OK" and "No thanks" buttons. If the user selects "OK," directly request the permission.
// If the user selects "No thanks," allow the user to continue without notifications.
} else {
// Directly ask for the permission
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}
}
}

@Composable
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/codeguru/gocapture/ui/ImageScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavHostController
import codeguru.gocapture.GoCaptureRepository
import codeguru.gocapture.MainActivity
import codeguru.gocapture.R
import coil.compose.AsyncImage
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -54,8 +55,10 @@ private fun UploadButton(
repository: GoCaptureRepository,
imageUri: String?
) {
val activity = LocalContext.current as MainActivity
FloatingActionButton(
onClick = {
activity.requestNotificationPermission()
setIsProcessing(true)
CoroutineScope(Dispatchers.IO).launch {
repository.processImage(Uri.parse(imageUri))
Expand Down

0 comments on commit e498249

Please sign in to comment.