Android通知工具类,含8.0通知适配,打开通知设置页面适配

先贴上代码,之后简单分析一下:
package com.lct.customview.wholeimport android.annotation.TargetApiimport android.app.NotificationChannelimport android.app.NotificationManagerimport android.app.PendingIntentimport android.content.Contextimport android.content.Intentimport android.graphics.BitmapFactoryimport android.net.Uriimport android.os.Buildimport android.provider.Settingsimport android.support.annotation.RequiresApiimport android.support.v4.app.NotificationCompatimport android.support.v4.app.NotificationManagerCompatimport android.support.v7.app.AlertDialog/*** Author:SkySmile* Date:2019/2/28* Description:App的通知渠道配置*/@TargetApi(Build.VERSION_CODES.N)object AppNotification {//通知ID//对于通知来说ID相同即为同一条通知,如果通知ID已存在,则更新通知内容,否则发送一条新的通知//这里为了每次都能发送一条新的通知,对ID进行累加private var id = 1//影视类通知渠道const val mediaChannelId = "0x1" //通知渠道IDconst val mediaChannelName = "影视" //通知渠道名称,显示在手机上该APP的通知管理中const val mediaChannelImportance = NotificationManager.IMPORTANCE_HIGH //通知渠道重要性//美食类通知渠道const val foodChannelId = "0x2" //通知渠道IDconst val foodChannelName = "美食" //通知渠道名称,显示在手机上该APP的通知管理中const val foodChannelImportance = NotificationManager.IMPORTANCE_DEFAULT //通知渠道重要性/*** 创建通知渠道** @param applicationContext 上下文* @param channelId 渠道ID* @param channelIdName 渠道名称,显示在手机上该APP的通知管理中* @param channelIdImportance 渠道重要程度*/fun createNotificationChannel(applicationContext: Context, channelId: String,channelIdName: String, channelIdImportance: Int) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManagerval notificationChannel = NotificationChannel(channelId, channelIdName,channelIdImportance)notificationManager.createNotificationChannel(notificationChannel)}}/*** 发送通知,根据需要进行扩展** @param context 上下文* @param channelId 渠道ID(必须对应已创建的渠道ID)* @param title 通知标题* @param text 通知内容* @param smallIcon 通知小图标(显示在状态栏中的),必须设置* @param largeIcon 通知大图标(下拉状态栏可见,显示在通知栏中),*注意:这里的图片ID不能是mipmap文件夹下的,因为BitmapFactory.decodeResource方法只能*获取到 drawable, sound, and raw resources;* @param pi 点击通知打开的页面**/fun sendNotification(context: Context, channelId: String, title: String,text: String, smallIcon: Int, largeIcon: Int, pi: PendingIntent) {//判断通知是否开启if (!isNotificationEnabled(context)) {AlertDialog.Builder(context).setTitle("提示").setMessage("是否开启通知?").setPositiveButton("确定") { _, _ ->openNotification(context)}.setNegativeButton("取消", null).show()return}//判断某个渠道的通知是否开启if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {if (!isNotificationChannelEnabled(context, channelId)) {val message = when (channelId) {mediaChannelId -> mediaChannelNamefoodChannelId -> foodChannelNameelse -> ""}AlertDialog.Builder(context).setTitle("提示").setMessage("是否开启${message}通知?").setPositiveButton("确定") { _, _ ->openNotificationChannel(context, channelId)}.setNegativeButton("取消", null).show()return}}//发送通知val notification = NotificationCompat.Builder(context, channelId).setContentTitle(title).setContentText(text).setWhen(System.currentTimeMillis()).setSmallIcon(smallIcon).setLargeIcon(BitmapFactory.decodeResource(context.resources, largeIcon)).setContentIntent(pi).setAutoCancel(true).build()val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManagernotificationManager.notify(id++, notification)}/*** 判断App通知是否开启* 注意这个方法判断的是通知总开关,如果APP通知被关闭,则其下面的所有通知渠道也被关闭*/fun isNotificationEnabled(context: Context): Boolean {val notificationManagerCompat = NotificationManagerCompat.from(context)return notificationManagerCompat.areNotificationsEnabled()}/*** 判断APP某个通知渠道的通知是否开启*/@RequiresApi(Build.VERSION_CODES.O)fun isNotificationChannelEnabled(context: Context, channelId: String): Boolean {val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManagerval channel = notificationManager.getNotificationChannel(channelId)return channel.importance != NotificationManager.IMPORTANCE_NONE}/*** 打开通知设置页面*/fun openNotification(context: Context) {val packageName = context.packageNameval uid = context.applicationInfo.uidval intent = Intent()when {Build.VERSION.SDK_INT >= Build.VERSION_CODES.O -> {intent.action = Settings.ACTION_APP_NOTIFICATION_SETTINGSintent.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)intent.putExtra(Settings.EXTRA_CHANNEL_ID, uid)}Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP -> {intent.action = "android.settings.APP_NOTIFICATION_SETTINGS"intent.putExtra("app_package", packageName)intent.putExtra("app_uid", uid)}Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT -> {intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGSintent.addCategory(Intent.CATEGORY_DEFAULT)intent.data = http://www.kingceram.com/post/Uri.parse("package:$packageName")}else -> intent.action = Settings.ACTION_SETTINGS}context.startActivity(intent)}/*** 打开通知渠道设置页面*/@RequiresApi(Build.VERSION_CODES.O)fun openNotificationChannel(context: Context, channelId: String) {val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId)context.startActivity(intent)}}