Özel bildirim düzeni oluşturma

Bildirimlerinizin farklı Android sürümlerinde en iyi şekilde görünmesini sağlamak için bildirimlerinizi oluşturmak üzere standart bildirim şablonunu kullanın. Bildiriminizde daha fazla içerik sağlamak isterseniz genişletilebilir bildirim şablonlarından birini kullanabilirsiniz.

Bununla birlikte, sistem şablonları ihtiyaçlarınızı karşılamıyorsa bildirim için kendi düzeninizi kullanabilirsiniz.

İçerik alanı için özel düzen oluşturma

Özel bir düzene ihtiyacınız varsa bildiriminize NotificationCompat.DecoratedCustomViewStyle uygulayabilirsiniz. Bu API, normalde başlık ve metin içeriğinin kapladığı içerik alanı için özel bir düzen sağlamanıza olanak tanır. Bu sırada bildirim simgesi, zaman damgası, alt metin ve işlem düğmeleri için sistem süslemelerini kullanmaya devam edebilirsiniz.

Bu API, aşağıdaki gibi temel bildirim düzeni üzerinde ilerleyerek genişletilebilir bildirim şablonlarına benzer şekilde çalışır:

  1. NotificationCompat.Builder ile temel bildirim oluşturun.
  2. setStyle() yöntemini çağırarak NotificationCompat.DecoratedCustomViewStyle örneğini iletin.
  3. RemoteViews örneği olarak özel düzeninizi şişirin.
  4. Daraltılmış bildirimin düzenini ayarlamak için setCustomContentView() kodunu çağırın.
  5. İsteğe bağlı olarak, genişletilmiş bildirim için farklı bir düzen ayarlamak üzere setCustomBigContentView() çağrısı da yapabilirsiniz.

Düzenleri hazırlama

small ve large düzenine ihtiyacınız vardır. Bu örnekte, small düzeni aşağıdaki gibi görünebilir:

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

    <TextView
        android:id="@+id/notification_title"
        style="@style/TextAppearance.Compat.Notification.Title"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Small notification, showing only a title" />
</LinearLayout>

Ayrıca large düzeni şöyle görünebilir:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/notification_title"
        style="@style/TextAppearance.Compat.Notification.Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Large notification, showing a title and a body." />

    <TextView
        android:id="@+id/notification_body"
        style="@style/TextAppearance.Compat.Notification.Line2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="This is the body. The height is manually forced to 300dp." />
</LinearLayout>

Bildirimi oluşturun ve gösterin

Düzenler hazır olduğunda, bunları aşağıdaki örnekte gösterildiği gibi kullanabilirsiniz:

Kotlin

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

// 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()

notificationManager.notify(666, customNotification)

Java

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// Get the layouts to use in the custom notification
RemoteViews notificationLayout = new RemoteViews(getPackageName(), R.layout.notification_small);
RemoteViews notificationLayoutExpanded = new RemoteViews(getPackageName(), R.layout.notification_large);

// Apply the layouts to the notification.
Notification customNotification = new NotificationCompat.Builder(context, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
        .setCustomContentView(notificationLayout)
        .setCustomBigContentView(notificationLayoutExpanded)
        .build();

notificationManager.notify(666, customNotification);

Bildirimin arka plan renginin cihazlara ve sürümlere göre değişebileceğini unutmayın. Aşağıdaki örnekte gösterildiği gibi, özel düzeninizde metin için TextAppearance_Compat_Notification ve başlık için TextAppearance_Compat_Notification_Title gibi Destek Kitaplığı stillerini uygulayın. Bu stiller renk varyasyonlarına uyum sağlar, böylece siyah üzerinde siyah veya beyaz üzerine beyaz metinle karşılaşmazsınız.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:text="@string/notification_title"
    android:id="@+id/notification_title"
    style="@style/TextAppearance.Compat.Notification.Title" />

Metniniz okunamaz hale gelebilir. Bu nedenle, RemoteViews nesnenizde arka plan resmi ayarlamaktan kaçının.

Kullanıcı bir uygulamayı kullanırken bir bildirimi tetiklediğinizde sonuç Şekil 1'e benzerdir:

Daraltılmış bir bildirimi gösteren resim
Şekil 1. Diğer uygulamalar kullanılırken küçük bir bildirim düzeni görüntülenir.

Genişletici oka dokunulduğunda, bildirim Şekil 2'de gösterildiği gibi genişletilir:

Sistem çubuğunda genişletilmiş bildirimi gösteren resim
Şekil 2. Diğer uygulamalar kullanılırken büyük bir bildirim düzeni görüntülenir.

Bildirim zaman aşımı süresi dolduğunda bildirim yalnızca sistem çubuğunda görünür (Şekil 3'e benzer):

Sistem çubuğunda daraltılmış bir bildirimi gösteren resim
Şekil 3. Küçük bildirim düzeninin sistem çubuğunda nasıl göründüğü.

Genişletici oka dokunulduğunda, bildirim Şekil 4'te gösterildiği gibi genişletilir:

Sistem çubuğunda genişletilmiş bildirimi gösteren resim
Şekil 4. Sistem çubuğunda büyük bir bildirim düzeni görünür.

Tamamen özel bir bildirim düzeni oluşturma

Bildiriminizin standart bildirim simgesi ve başlığıyla dekore edilmesini istemiyorsanız önceki adımları uygulayın ancak setStyle() çağrısını yapmayın.