Notification channel - Muhammad al-xorazmiy nomidagi toshkent axborot texnologiyalari universiteti urganch filiali...

|
Muhammad al-xorazmiy nomidagi toshkent axborot texnologiyalari universiteti urganch filiali mobil ilova yaratish
|
bet | 3/7 | Sana | 19.05.2024 | Hajmi | 239,8 Kb. | | #243587 |
Bog'liq ANDROID NOTIFICATIONSNotification channel.
Notification channel bu ilovamiz boshqariladigan guruhlarga yuboradigan notification-larni guruhlash usulidir.
Android 8.0 (API darajasi 26) va undan keyingi versiyalarda notification-lar channel-ga tegishli boʻlishi kerak va har bir channel uchun siz ushbu channel-dagi barcha notification-larga tegishli vizual va akustik xatti-harakatlarni oʻrnatishingiz mumkin.
Buning yordamida user-lar ushbu sozlamalarni o’zgartirishda va ilovangizdagi notification channel intruziv, ko’rinadigan (visible) va hokazo bo’lishi kerakligini hal qilishda ko’proq moslashuvchanlikka ega.
Dasturchi nuqtai nazaridan, notification-ni ko’rsatishdan oldin siz avval channel yaratishingiz kerak. NotificationManager-ga havola olishdan boshlang. Shundan so’ng, siz Android 8.0 yoki undan yuqori versiyadagi qurilmada ishlayotganingizga ishonch hosil qilishingiz kerak bo’ladi — buning uchun siz kerakli tekshiruvlarni o’tkazishingiz kerak, agar kerak bo’lsa, channel yarating va uni qo’shing.
Mana, channel yaratish, uning parametrlarini sozlash va NotificationManager-ga qo’shishga misol :
companion object {
private const val NOTIFICATION_ID = 112
private const val PRIMARY_CHANNEL_ID = "primary_notification_channel"
}
private lateinit var notificationManager: NotificationManager
private fun createNotificationChannel() {
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
PRIMARY_CHANNEL_ID,
"Messages",
NotificationManager.IMPORTANCE_HIGH
)
channel.enableLights(true)
channel.lightColor = Color.RED
channel.enableVibration(true)
channel.description = "Messages Notification"
notificationManager.createNotificationChannel(channel)
}
}
Notification-ni ko’rsatish.
Notification-ni ko’rsatish uchun avval uni build qilishingiz kerak, chunki notification juda ko’p parametrlarga ega bo’lgan narsadir. Buni NotificationCompat.Builder orqali amalga oshiriladi.
Bu builder-ga notification bog‘lanadigan context va channel kerak bo‘ladi. Ushbu builder-dan foydalanib, siz notification-ning turli parametrlarini o’rnatasiz.
Notification yaratishni tugatganingizdan so’ng, uni ko’rsatish uchun siz NotificationManager yoki NotificationManagerCompat yordamida kodni yanada samarali, ixcham va tozaroq qilishingiz mumkin.
Ikkalasi ham notification obyektini (biz yaratgan) va ushbu notification-ni identifikatsiya qiluvchi notification ID-ni talab qiladi. Shunday qilib, agar biz uni yangilashni yoki bekor qilishni xohlasak, unga havola (reference) olishimiz mumkin.
Bu yerda notification yaratish va uni ko’rsatishga misol :
// creating the notification and its parameters.
val builder = NotificationCompat.Builder(this, PRIMARY_CHANNEL_ID).apply {
setSmallIcon(R.drawable.ic_android)
setContentTitle("Notification Article")
setContentText("Learn How To Manage And Display Notifications in Android")
setPriority(NotificationCompat.PRIORITY_DEFAULT)
}
// displaying the notification with NotificationManagerCompat.
with(NotificationManagerCompat.from(this)) {
notify(NOTIFICATION_ID, builder.build())
}
/* or you can use the notification manager object.
notificationManager.notify(NOTIFICATION_ID, notification.build())
*/
|
| |