Android 判断当前应用是否开启消息通知

NotificationUtil.kt

object NotificationUtil {
    /**
     * 打开手机设置页面
     * @param context Context
     */
    fun setNotification(context: Context) {
        val enabled = isNotificationEnabled(context)
        if (!enabled) {
            val localIntent = Intent()
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                localIntent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGS
                localIntent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
                localIntent.putExtra("app_uid", context.applicationInfo.uid)
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                localIntent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
                localIntent.putExtra("app_package", context.packageName)
                localIntent.putExtra("app_uid", context.applicationInfo.uid)
            } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                localIntent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
                localIntent.addCategory(Intent.CATEGORY_DEFAULT)
                localIntent.data = Uri.parse("package:" + context.packageName)
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                localIntent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
                localIntent.data = Uri.fromParts("package", context.packageName, null)
            } else {
                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                if (Build.VERSION.SDK_INT >= 9) {
                    localIntent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"
                    localIntent.data = Uri.fromParts("package", context.packageName, null)
                } else if (Build.VERSION.SDK_INT <= 8) {
                    localIntent.action = Intent.ACTION_VIEW
                    localIntent.setClassName(
                        "com.android.settings",
                        "com.android.setting.InstalledAppDetails"
                    )
                    localIntent.putExtra(
                        "com.android.settings.ApplicationPkgName",
                        context.packageName
                    )
                }
            }
            context.startActivity(localIntent)
        }
    }

    /**
     * 判断当前app在手机中是否开启了允许消息推送
     * @param mContext Context
     * @return Boolean
     */
    fun isNotificationEnabled(mContext: Context): Boolean {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val mNotificationManager =
                mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            var channel = mNotificationManager.getNotificationChannel(PortalCode.ChannelId)
            !(!mNotificationManager.areNotificationsEnabled() || channel.importance == NotificationManager.IMPORTANCE_NONE)
        } else if (Build.VERSION.SDK_INT >= 24) {
            val mNotificationManager =
                mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            mNotificationManager.areNotificationsEnabled()
        } else if (Build.VERSION.SDK_INT >= 19) {
            val CHECK_OP_NO_THROW = "checkOpNoThrow"
            val OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION"
            val appOps = mContext.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
            val appInfo = mContext.applicationInfo
            val pkg = mContext.applicationContext.packageName
            val uid = appInfo.uid
            try {
                val appOpsClass = Class.forName(AppOpsManager::class.java.name)
                val checkOpNoThrowMethod = appOpsClass.getMethod(
                    CHECK_OP_NO_THROW, Integer.TYPE,
                    Integer.TYPE, String::class.java
                )
                val opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION)
                val value = opPostNotificationValue[Int::class.java] as Int
                (checkOpNoThrowMethod.invoke(appOps, value, uid, pkg) as Int
                        == AppOpsManager.MODE_ALLOWED)
            } catch (e: ClassNotFoundException) {
                true
            } catch (e: NoSuchMethodException) {
                true
            } catch (e: NoSuchFieldException) {
                true
            } catch (e: InvocationTargetException) {
                true
            } catch (e: IllegalAccessException) {
                true
            } catch (e: RuntimeException) {
                true
            }
        } else {
            true
        }
    }
}

调用:

if (NotificationUtil.isNotificationEnabled(this)) {
    //当前app允许消息通知
} else {
    AlertDialog.Builder()
        .message("为了您能收到推送通知请点击确定开始设置")
        .okButtonText("确定")
        .build().apply {
            setOkBtnClickListener {
                NotificationUtil.setNotification(this@CurMainActivity)
            }
            show(this@CurMainActivity)
        }
}

你可能感兴趣的:(代码块,android,消息通知,Notification)