Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android notification permission opens notification settings #111

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}