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

先贴上代码,之后简单分析一下:

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
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值