إنشاء تنسيق إشعارات مخصص

لعرض إشعاراتك بأفضل شكل ممكن على مختلف إصدارات Android، يمكنك استخدام نموذج الإشعارات العادية لإنشاء الإشعارات. إذا أردت تقديم المزيد من المحتوى في الإشعار، يمكنك استخدام أحد نماذج الإشعارات القابلة للتوسيع.

ومع ذلك، إذا كانت قوالب النظام لا تلبي احتياجاتك، فيمكنك استخدام تنسيقك الخاص للإشعار.

إنشاء تخطيط مخصص لمنطقة المحتوى

إذا كنت بحاجة إلى تنسيق مخصّص، يمكنك تطبيق NotificationCompat.DecoratedCustomViewStyle على الإشعار. تتيح لك واجهة برمجة التطبيقات هذه توفير تنسيق مخصّص لمنطقة المحتوى التي يشغلها العنوان والمحتوى النصي عادةً، مع استخدام زخارف النظام لرمز الإشعار والطابع الزمني والنص الفرعي وأزرار الإجراءات.

تعمل واجهة برمجة التطبيقات هذه بشكل مشابه لنماذج الإشعارات القابلة للتوسيع من خلال الاعتماد على التنسيق الأساسي للإشعارات على النحو التالي:

  1. أنشئ إشعارًا أساسيًا باستخدام NotificationCompat.Builder.
  2. استدعِ setStyle()، وامنحه مثالاً على NotificationCompat.DecoratedCustomViewStyle.
  3. يمكنك تضخيم التنسيق المخصّص كمثيل لـ RemoteViews.
  4. يمكنك الاتصال بالرقم setCustomContentView() لضبط تنسيق الإشعار المصغّر.
  5. يمكنك اختياريًا استدعاء الرمز setCustomBigContentView() أيضًا لضبط تنسيق مختلف للإشعار الموسّع.

تجهيز التنسيقات

يجب استخدام تنسيق small وlarge. في هذا المثال، قد يبدو تنسيق small على النحو التالي:

<?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>

وقد يبدو تنسيق large على النحو التالي:

<?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>

إنشاء الإشعار وعرضه

بعد أن تصبح التنسيقات جاهزة، يمكنك استخدامها كما هو موضَّح في المثال التالي:

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);

ملاحظة: يمكن أن يختلف لون الخلفية للإشعار على مستوى الأجهزة والإصدارات المختلفة. طبِّق أنماط مكتبة الدعم مثل TextAppearance_Compat_Notification للنص و TextAppearance_Compat_Notification_Title للعنوان في التنسيق المخصّص، كما هو موضّح في المثال التالي. تتكيف هذه الأنماط مع تباينات الألوان حتى لا ينتهي بك الأمر بنص أسود على أسود أو أبيض على أبيض.

<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" />

تجنَّب ضبط صورة خلفية على عنصر RemoteViews، لأنّ النص قد يصبح غير قابل للقراءة.

عند تشغيل إشعار أثناء استخدام المستخدم للتطبيق، تكون النتيجة مشابهة للشكل 1:

صورة تعرض إشعارًا مصغّرًا
الشكل 1. يظهر تنسيق صغير للإشعارات أثناء استخدام تطبيقات أخرى.

يؤدي النقر على سهم التوسيع إلى توسيع الإشعار، كما هو موضح في الشكل 2:

صورة تعرض إشعارًا موسَّعًا في شريط النظام
الشكل 2. يظهر تنسيق إشعارات كبير أثناء استخدام تطبيقات أخرى.

بعد انتهاء مهلة الإشعار، لا يظهر الإشعار إلا في شريط النظام، الذي يشبه الشكل 3:

صورة تعرض إشعارًا مصغّرًا في شريط النظام
الشكل 3. كيفية ظهور التنسيق الصغير للإشعارات في شريط النظام

يؤدي النقر على سهم التوسيع إلى توسيع الإشعار، كما هو موضح في الشكل 4:

صورة تعرض إشعارًا موسَّعًا في شريط النظام
الشكل 4. سيظهر تنسيق إشعار كبير في شريط النظام.

إنشاء تنسيق إشعارات مخصص بالكامل

إذا كنت لا تريد أن يظهر إشعارك برمز الإشعار العاديين ورأسه، اتّبِع الخطوات السابقة ولكن لا تستدعي setStyle().