先贴上代码,之后简单分析一下:
package com.lct.customview.whole
import android.annotation.TargetApi
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.support.annotation.RequiresApi
import android.support.v4.app.NotificationCompat
import android.support.v4.app.NotificationManagerCompat
import 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" //通知渠道ID
const val mediaChannelName = "影视" //通知渠道名称,显示在手机上该APP的通知管理中
const val mediaChannelImportance = NotificationManager.IMPORTANCE_HIGH //通知渠道重要性
//美食类通知渠道
const val foodChannelId = "0x2" //通知渠道ID
const 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 NotificationManager
val notificationChannel = NotificationChannel(channelId, channelIdName,
channelIdImportance)
notificat