כדי שההתראות ייראו הכי טוב בגרסאות שונות של Android, צריך להשתמש בתבנית ההתראות הרגילה כדי ליצור את ההתראות. אם רוצים לספק יותר תוכן בהתראה, אפשר להשתמש באחת מתבניות ההתראות הניתנות להרחבה.
עם זאת, אם התבניות של המערכת לא מתאימות לצרכים שלכם, אתם יכולים להשתמש בפריסה משלכם להתראה.
יצירת פריסה בהתאמה אישית לאזור התוכן
אם אתם צריכים פריסה בהתאמה אישית, אתם יכולים להחיל את NotificationCompat.DecoratedCustomViewStyle על ההתראה. ה-API הזה מאפשר לכם לספק פריסה בהתאמה אישית לאזור התוכן שבדרך כלל תופסים הכותרת והטקסט, ועדיין להשתמש בעיטורי המערכת לסמל ההתראה, חותמת הזמן, הטקסט המשני וכפתורי הפעולה.
ממשק ה-API הזה פועל באופן דומה לתבניות ההתראות הניתנות להרחבה, על ידי בנייה על פריסת ההתראות הבסיסית באופן הבא:
- יוצרים התראה בסיסית באמצעות
NotificationCompat.Builder. - קוראים לפונקציה
setStyle()ומעבירים לה מופע שלNotificationCompat.DecoratedCustomViewStyle. - מנפחים את הפריסה בהתאמה אישית כמופע של
RemoteViews. - קוראים ל-
setCustomContentView()כדי להגדיר את הפריסה של ההתראה המכווצת. - אפשר גם לקרוא ל-
setCustomBigContentView()כדי להגדיר פריסה שונה להתראה המורחבת.
הכנת הפריסות
צריך פריסה של small ו-large. בדוגמה הזו, הפריסה של small עשויה להיראות כך:
<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().