通知工具类

package com.les.demo.notification

import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Context.NOTIFICATION_SERVICE
import android.content.Intent
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Build
import android.provider.Settings
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import com.les.demo.R
import com.les.demo.dialog.DialogUtil

/**
 * @author: liang_farone
 * @date: 2021/7/4
 */
class NotificationUtil private constructor() {
    private var pendingIndent: PendingIntent? = null
    private var mIntent: Intent? = null
    private var notificationChannel: NotificationChannel? = null
    private var builder: NotificationCompat.Builder? = null
    private var notificationManager: NotificationManager? = null

    @RequiresApi(api = Build.VERSION_CODES.O)
    private fun createChannels(
        builder: NotificationCompat.Builder,
        notificationManager: NotificationManager,
        channelId: String,
        channelName: String,
        importance: Int
    ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationChannel =
                NotificationChannel(
                    channelId,
                    channelName,
                    importance
                )
            notificationManager.createNotificationChannel(notificationChannel!!)
            builder.setChannelId(channelId)
        }
    }

    /**
     * 判断通知渠道是否开启(单个消息渠道)
     * @param channelID 渠道 id
     * @return true 开启
     */
    @RequiresApi(api = Build.VERSION_CODES.O)
    private fun checkNotificationsChannelEnabled(channelID: String): Boolean {
        val channel = notificationManager?.getNotificationChannel(channelID)
        return channel?.importance == NotificationManager.IMPORTANCE_HIGH
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun showNotification(
        context: Context,
        channelID: String,
        channelName: String,
        importance: Int
    ) {
        if (null == builder) {
            builder = NotificationCompat.Builder(context)
        }
        if (null == notificationManager) {
            notificationManager =
                context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        }
        if (!checkNotificationsChannelEnabled(channelID)) {
            DialogUtil.getInstance()
                .showCommonDialog(context, "是否去开启悬浮窗权限?",
                    "确定",
                    "取消",
                    { _, _ ->
                        goToSetting(context, channelID)
                        DialogUtil.getInstance().recycle()
                    },
                    { _, _ -> DialogUtil.getInstance().recycle() })

        } else {
            createChannels(
                builder!!,
                notificationManager!!,
                channelID,
                channelName,
                importance
            )

            mIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"))
            pendingIndent = PendingIntent.getActivity(context, 0, mIntent, 0)
            builder?.setContentIntent(pendingIndent)
            builder?.setSmallIcon(R.mipmap.ic_launcher)
            builder?.setLargeIcon(
                BitmapFactory.decodeResource(
                    context.resources,
                    R.mipmap.ic_launcher
                )
            )
            builder?.setAutoCancel(true)
            builder?.setContentTitle("你好,我叫哈哈哈")
            notificationManager?.notify(1, builder?.build())
        }
    }

    @RequiresApi(Build.VERSION_CODES.O)
    private fun goToSetting(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)
    }

    fun recycle() {
        INSTANCE = null
    }

    companion object {
        private var INSTANCE: NotificationUtil? = null

        fun getInstance(): NotificationUtil {
            if (null == INSTANCE) {
                INSTANCE = NotificationUtil()
            }
            return INSTANCE!!
        }

        class ChannelName {
            companion object {
                const val HIGH_CHANNEL_DESCRIPTION = "重要信息"
            }
        }

        class ChannelID {
            companion object {
                const val HIGH_CHANNEL_ID = "789"
            }
        }
    }
}
package com.les.demo.dialog

import android.content.Context
import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog

/**
 * @author: liang_farone
 * @date: 2021/7/4
 */
class DialogUtil private constructor() {
    private var builder: AlertDialog.Builder? = null

    companion object {
        private var INSTANCE: DialogUtil? = null

        fun getInstance(): DialogUtil {
            if (null == INSTANCE) {
                INSTANCE = DialogUtil()
            }
            return INSTANCE!!
        }
    }

    fun showCommonDialog(
        context: Context,
        title: String,
        positiveText: String,
        negativeText: String,
        positiveListener: DialogInterface.OnClickListener,
        negativeListener: DialogInterface.OnClickListener
    ) {
        if (null == builder) {
            builder = AlertDialog.Builder(context)
        }
        builder?.setTitle(title)
        builder?.setPositiveButton(positiveText, positiveListener)
        builder?.setNegativeButton(negativeText, negativeListener)
        builder?.setCancelable(false)
        builder?.show()
    }

    fun recycle() {
        INSTANCE = null
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值