Skip to content

Commit

Permalink
Merge pull request #111 from Joe3112/feature/android-notification-set…
Browse files Browse the repository at this point in the history
…tings

Android notification permission opens notification settings
  • Loading branch information
MohamedRejeb committed Jul 10, 2024
2 parents c047d29 + 0edfc3f commit aa64fbc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.provider.Settings
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.Stable
Expand Down Expand Up @@ -106,10 +107,14 @@ internal actual class MutablePermissionState(
actual override fun openAppSettings() {
if (context == null) return

val intent = Intent().apply {
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
data = Uri.fromParts("package", context.packageName, null)
flags = Intent.FLAG_ACTIVITY_NEW_TASK
val intent = when (permission) {
Permission.Notification -> if (supportsNotificationSettings()) {
createAppNotificationsIntent(context)
} else {
createAppSettingsIntent(context)
}

else -> createAppSettingsIntent(context)
}
context.startActivity(intent)
}
Expand All @@ -132,4 +137,20 @@ internal actual class MutablePermissionState(
PermissionStatus.Denied(activity.shouldShowRationale(androidPermission))
}
}

@RequiresApi(Build.VERSION_CODES.O)
private fun createAppNotificationsIntent(context: Context) =
Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
}

private fun supportsNotificationSettings() =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU

private fun createAppSettingsIntent(context: Context) =
Intent().apply {
action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
data = Uri.fromParts("package", context.packageName, null)
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,24 @@ private fun PermissionItem(permission: Permission) {
)
}



Spacer(modifier = Modifier.height(16.dp))

if (permission == Permission.Notification) {
Button(
onClick = {
permissionState.openAppSettings()
},
) {
Text(
text = "Open Notification Settings",
)
}

Spacer(modifier = Modifier.height(16.dp))
}


HorizontalDivider()
}

0 comments on commit aa64fbc

Please sign in to comment.