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