لعرض إشعاراتك بأفضل شكل ممكن على مختلف إصدارات Android، يمكنك استخدام نموذج الإشعارات العادية لإنشاء الإشعارات. إذا أردت تقديم المزيد من المحتوى في الإشعار، يمكنك استخدام أحد نماذج الإشعارات القابلة للتوسيع.
ومع ذلك، إذا كانت قوالب النظام لا تلبي احتياجاتك، فيمكنك استخدام تنسيقك الخاص للإشعار.
إنشاء تخطيط مخصص لمنطقة المحتوى
إذا كنت بحاجة إلى تنسيق مخصّص، يمكنك تطبيق
NotificationCompat.DecoratedCustomViewStyle
على الإشعار. تتيح لك واجهة برمجة التطبيقات هذه توفير تنسيق مخصّص لمنطقة المحتوى التي يشغلها العنوان والمحتوى النصي عادةً، مع استخدام زخارف النظام لرمز الإشعار والطابع الزمني والنص الفرعي وأزرار الإجراءات.
تعمل واجهة برمجة التطبيقات هذه بشكل مشابه لنماذج الإشعارات القابلة للتوسيع من خلال الاعتماد على التنسيق الأساسي للإشعارات على النحو التالي:
- أنشئ إشعارًا أساسيًا
باستخدام
NotificationCompat.Builder
. - استدعِ
setStyle()
، وامنحه مثالاً علىNotificationCompat.DecoratedCustomViewStyle
. - يمكنك تضخيم التنسيق المخصّص كمثيل لـ
RemoteViews
. - يمكنك الاتصال بالرقم
setCustomContentView()
لضبط تنسيق الإشعار المصغّر. - يمكنك اختياريًا استدعاء الرمز
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:
يؤدي النقر على سهم التوسيع إلى توسيع الإشعار، كما هو موضح في الشكل 2:
بعد انتهاء مهلة الإشعار، لا يظهر الإشعار إلا في شريط النظام، الذي يشبه الشكل 3:
يؤدي النقر على سهم التوسيع إلى توسيع الإشعار، كما هو موضح في الشكل 4:
إنشاء تنسيق إشعارات مخصص بالكامل
إذا كنت لا تريد أن يظهر إشعارك برمز الإشعار العاديين ورأسه،
اتّبِع الخطوات السابقة ولكن لا تستدعي setStyle()
.