안드로이드/Tip
[Android] 알림 설정 이동 하기!
Debbi
2021. 10. 28. 23:03
728x90
앱 알림 설정 화면으로 이동하는 방법이 Oreo 버전 이상 이하 버전 다르게 처리해 주어야 합니다!
fun presentNotificationSetting(context: Context) {
val intent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationSettingOreo(context)
} else {
notificationSettingOreoLess(context)
}
try {
context.startActivity(intent)
}catch (e: ActivityNotFoundException) {
e.printStackTrace()
}
}
@RequiresApi(Build.VERSION_CODES.O)
fun notificationSettingOreo(context: Context): Intent {
return Intent().also { intent ->
intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
}
fun notificationSettingOreoLess(context: Context): Intent {
return Intent().also { intent ->
intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
intent.putExtra("app_package", context.packageName)
intent.putExtra("app_uid", context.applicationInfo?.uid)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
}
추가로 앱 알림 설정 여부를 가져오는 방법 입니다!
NotificationManagerCompat.from(context).areNotificationsEnabled()