안드로이드 알림

redbird's home
둘러보기로 이동 검색으로 이동

안드로이드 알림 권한 얻기 설정창 열기

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings

fun openAppNotificationSettings(context: Context) {
    val intent = Intent().apply {
        when {
            // Android 8.0 (API 26) 이상: ACTION_APP_NOTIFICATION_SETTINGS 사용
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {
                action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
                putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
                // 또는 특정 알림 채널 설정으로 이동하려면 EXTRA_CHANNEL_ID를 추가할 수 있습니다.
                // putExtra(Settings.EXTRA_CHANNEL_ID, channelId) 
            }
            // Android 5.0 (API 21) ~ 7.1 (API 25): ACTION_APPLICATION_DETAILS_SETTINGS 사용
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> {
                action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
                data = Uri.fromParts("package", context.packageName, null)
            }
            // 그 이하 버전에서는 일반 앱 정보 화면으로 이동 (알림 설정 옵션이 없을 수 있음)
            else -> {
                action = Intent.ACTION_VIEW
                data = Uri.fromParts("package", context.packageName, null)
            }
        }
        addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) // 새 태스크에서 설정 화면 열기
    }
    context.startActivity(intent)
}