老树发新芽。最近重构了一老项目,真是障碍重重啊
Notification
直接CV 就能用 , 因为时间有限 ,以下代码是从自己的项目中截取的,有很多配置只适合我自己的项目,如果问题留言沟通
val NOTIFICATION_FLAG = 0X11
lateinit var mNotificationManager: NotificationManager
lateinit var builder: Notification.Builder
private fun initNotification() {
mNotificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
builder = Notification.Builder(this.applicationContext)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val CHANNEL_ONE_ID = "com.zhf"
val CHANNEL_ONE_NAME = "Channel ONE"
builder.setChannelId(CHANNEL_ONE_ID)
var notificationChannel = NotificationChannel(
CHANNEL_ONE_ID,
CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH
)
notificationChannel.enableLights(false)
notificationChannel.setSound(null, null)
notificationChannel.lightColor = Color.RED
notificationChannel.setShowBadge(true)
notificationChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
mNotificationManager.createNotificationChannel(notificationChannel)
}
val pendingIntent =
PendingIntent.getActivity(this, 0, Intent(this, MainActivity::class.java), 0)
builder.setContentIntent(pendingIntent)
.setTicker("您有一个notification")
.setContentTitle("工作待命状态")
.setSmallIcon(R.mipmap.logo)
.setContentText("休息时间")
.setWhen(System.currentTimeMillis())
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(false)
val notification: Notification = builder.build()
notification.flags = notification.flags or
Notification.FLAG_AUTO_CANCEL or
NotificationCompat.FLAG_ONGOING_EVENT or
NotificationCompat.FLAG_NO_CLEAR
mNotificationManager.notify(NOTIFICATION_FLAG, notification)
startForeground(NOTIFICATION_FLAG, notification)
}
fun addMyNotification() {
builder.setContentTitle(getTitle())
.setContentText(getText())
.setAutoCancel(false)
val notification = builder.build()
notification.flags = notification.flags or
Notification.FLAG_AUTO_CANCEL or
NotificationCompat.FLAG_ONGOING_EVENT or
NotificationCompat.FLAG_NO_CLEAR
mNotificationManager.notify(NOTIFICATION_FLAG, notification)
}
|