Even after following the official documentation official documentation
Also After adding style .setStyle(new Notification.DecoratedCustomViewStyle())
from the DecoratedStyle above android 12
Here the the code for custom_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="44dp"
android:padding="10dp" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TextAppearance.Compat.Notification.Title"
android:textColor="@color/black"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="@color/black"
android:layout_below="@id/title"/>
</RelativeLayout>
Here is the code of MainActivity.kt
class MainActivity : AppCompatActivity() {
@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val CHANNEL_ID = "my_channel_01"
val remoteViews = RemoteViews(packageName, R.layout.custom_notification)
remoteViews.setTextViewText(R.id.title, "Title")
remoteViews.setTextViewText(R.id.text, "Text for the notification")
val mBuilder: NotificationCompat.Builder =
NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(remoteViews)
.setCustomBigContentView(remoteViews)
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
val notificationManager = getSystemService(
Context.NOTIFICATION_SERVICE
) as NotificationManager
notificationManager.notify(111, mBuilder.build())
}
In Manifest.xml
> To show notifications above Android 12
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
> I think its useless, will check it again if this permission is needed or not
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
Testing on Android 14 by manually giving notification permission from the app settings
I don’t even think in official documentation of custom layout for notifications , these things are missing
By adding below code in MainActivity.kt
val CHANNEL_NAME = "Notification Channel"
private val notificationManager by lazy { getSystemService(NotificationManager::class.java) }
val notificationChannel =
NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(notificationChannel)