檢查是否有通知欄權限
NotificationManagerCompat.from(context).areNotificationsEnabled()
打開通知欄權限設置頁
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
object NotificationUtil {
//調用該方法獲取是否開啓通知欄權限
fun goToNotificationSetting(context: Context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//這種方案適用於 API 26, 即8.0(含8.0)以上可以用
try {
val intent = Intent()
intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
intent.putExtra(Settings.EXTRA_CHANNEL_ID, context.applicationInfo.uid)
context.startActivity(intent)
} catch (e: Exception) {
toPermissionSetting(context)
}
} else {
toPermissionSetting(context)
}
}
/**
* 跳轉到權限設置
*
* @param activity
*/
private fun toPermissionSetting(activity: Context) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
toSystemConfig(activity)
} else {
try {
toApplicationInfo(activity)
} catch (e: java.lang.Exception) {
L.printStackTrace(e)
toSystemConfig(activity)
}
}
}
/**
* 應用信息界面
*
* @param activity
*/
private fun toApplicationInfo(activity: Context) {
try {
val localIntent = Intent()
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
localIntent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
localIntent.data = Uri.fromParts("package", activity.packageName, null)
activity.startActivity(localIntent)
} catch (e: java.lang.Exception) {
L.printStackTrace(e)
}
}
/**
* 系統設置界面
*
* @param activity
*/
private fun toSystemConfig(activity: Context) {
try {
val intent = Intent(Settings.ACTION_SETTINGS)
activity.startActivity(intent)
} catch (e: java.lang.Exception) {
L.printStackTrace(e)
}
}
}