DecoratedCustomViewStyle
// Get the layouts to use in the custom notification
val notificationLayout = RemoteViews(packageName, R.layout.notification_small)
val notificationLayoutExpanded = RemoteViews(packageName, R.layout.notification_large)
// Apply the layouts to the notification
val customNotification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(notificationLayout)
.setCustomBigContentView(notificationLayoutExpanded)
.build()
Custom Remoteview
https://developer.android.com/training/notify-user/custom-notification
object NotificationUtil {
enum class NotificationStyle(
var iconId: Int,
var layoutId: Int,
) {
Style1(R.drawable.lib_widget_icon_notification_launcher,
R.layout.lib_widget_notification_style_1),
Style2(R.drawable.lib_widget_icon_notification_launcher,
R.layout.lib_widget_notification_style_2),
}
fun clearNotification(context: Context, notificationId: Int) {
NotificationManagerCompat.from(context).cancel(notificationId)
}
fun notification(
context: Context,
notificationId: Int,
channelId: String,
paddingIntent: PendingIntent,
style: NotificationStyle,
) {
notification(context,
notificationId,
channelId,
paddingIntent,
style.iconId,
style.layoutId)
}
fun notification(
context: Context,
notificationId: Int,
channelId: String,
paddingIntent: PendingIntent,
iconId: Int,
layoutId: Int,
) {
val notificationLayoutExpanded = RemoteViews(context.packageName, layoutId)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = context.getString(R.string.lib_widget_notification_name)
val descriptionText = context.getString(R.string.lib_widget_notification_descript)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(channelId, name, importance).apply {
description = descriptionText
}
val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
val customNotification = NotificationCompat.Builder(context, channelId)
.setSmallIcon(iconId)
.setGroup(context.packageName)
.setCustomBigContentView(notificationLayoutExpanded)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(paddingIntent)
.setAutoCancel(false)
.build().apply { flags = Notification.FLAG_ONGOING_EVENT //permanent }
NotificationManagerCompat.from(context).notify(notificationId, customNotification)
}
}
val paddingIntent = PendingIntent.getActivity(this, 0,
Intent(this, DemoActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT)
NotificationUtil.notification(this, 1, "1", paddingIntent,NotificationUtil.NotificationStyle.Style1)
NotificationUtil.notification(this, 2, "2", paddingIntent,NotificationUtil.NotificationStyle.Style2)
Repeat & .9 png
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:src="@drawable/lib_widget_icon_notification_background_style3"
android:tileMode="repeat"
android:antialias="true"
android:dither="false"
android:filter="false"
android:gravity="left"
android:autoMirrored="true" />
.9.png