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
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)
}
}
@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
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
}
}