简体   繁体   中英

Custom notification is not showing in the android notification panel

I'm trying to implement a custom notification in my project using Kotlin language. But, it does not show in the notification panel, although its's default one is working on button click. Suppose I remove the ContentTitle and ContentText and set custom content, then only the notification rings and nothing shows.

Funtion:

@RequiresApi(Build.VERSION_CODES.O)
@SuppressLint("RemoteViewLayout")
private fun showNotification() {


    val intent = Intent(this, MainActivity::class.java)
    val pendingIntent = TaskStackBuilder.create(this).run {
        addNextIntentWithParentStack(intent)
        getPendingIntent(0, PendingIntent.FLAG_IMMUTABLE)
    }

    val notificationManager =
        this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    val channel = NotificationChannel(
        CHANNEL_ID,
        CHANNEL_NAME,
        NotificationManager.IMPORTANCE_HIGH
    )

    val contentView = RemoteViews(packageName, R.layout.notification_small)
    contentView.setTextViewText(R.id.tvHead, "Woohlah")
    contentView.setTextViewText(R.id.tvDes, "Working Very well")

    val builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.ic_notify_status)
        .setContentTitle("Yahoo")
        .setContentText("Koi Mujhe Junglee Kahe")
        .setStyle(NotificationCompat.DecoratedCustomViewStyle())
        .setCustomContentView(contentView)
        .setCustomBigContentView(contentView)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)

    notificationManager.createNotificationChannel(channel)
    notificationManager.notify(NOTIFICATION_ID, builder.build())
}

XML code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<ImageView
    android:id="@+id/imageViewNotification"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_centerVertical="true"
    android:contentDescription="@string/dot"
    android:padding="20dp"
    android:src="@drawable/ic_notify_status" />

<TextView
    android:id="@+id/tvHead"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="10dp"
    android:layout_toEndOf="@+id/imageViewNotification"
    android:paddingTop="10dp"
    android:textColor="@color/white"
    android:textSize="18sp"
    android:textStyle="bold"
    tools:text="xxx" />

<TextView
    android:id="@+id/tvDes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvHead"
    android:layout_marginHorizontal="10dp"
    android:layout_toEndOf="@+id/imageViewNotification"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:textColor="@color/white"
    android:textSize="14sp"
    tools:text="xxx" />

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/tvCount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:padding="10dp"
    android:textColor="@color/black"
    android:textSize="14sp"
    tools:text="0" />
</RelativeLayout>

As per the documentation, not all layouts and widgets are supported by RemoteViews. Your AppCompatTextView is causing the issue. Replace it with a simple TextView .

Also,

Beware that the background color for the notification can vary across different devices and versions. So you should always apply support library styles such as TextAppearance_Compat_Notification for the text and TextAppearance_Compat_Notification_Title for the title in your custom layout. These styles adapt to the color variations so you don't end up with black-on-black or white-on-white text.

Documentation:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM