Junior
Androidda qanday bildirishnoma yaratish mumkin?
sobes.tech AI
AIdan javob
Androidda bildirim yaratish uchun quyidagi bosqichlarni bajarish kerak:
- Bildirim kanali yaratish (API 26 va yuqori uchun).
NotificationCompat.Builderyordamida bildirishnomani qurish.- Bildirishnomani bildirishlar menejeri orqali ko'rsatish.
// Bildirishnomalar kanali yaratish (API 26+)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String channelId = "my_channel_id";
CharSequence channelName = "Mening Kanalim";
String channelDescription = "Mening Kanalim uchun tavsif";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
channel.setDescription(channelDescription);
// Kanalni tizimga ro'yxatdan o'tkazish
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
// Bildirishnoma quruvchisini yaratish
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "my_channel_id") // Kanal ID sini ko'rsating
.setSmallIcon(R.drawable.ic_notification) // Bildirishnoma ikonkasi
.setContentTitle("Bildirishnoma sarlavhasi") // Sarlavha
.setContentText("Bildirishnoma matni") // Matn
.setPriority(NotificationCompat.PRIORITY_DEFAULT); // Prioritet
// Bildirishnoma bosilganda amal qilish uchun PendingIntent yaratish
// Masalan, faoliyatni ochish
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); // API S+ uchun bayroq
builder.setContentIntent(pendingIntent);
// Bildirishnomani ko'rsatish
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
int notificationId = 1; // Unikal ID
notificationManagerCompat.notify(notificationId, builder.build());
Asosiy komponentlar:
- Kichkina ikonka:
setSmallIcon(), holat panelida ko'rsatiladi. - Sarlavha:
setContentTitle(), asosiy matn. - Matn:
setContentText(), qo'shimcha matn. - Bildirishnoma kanali (API 26+): Umumiy sozlamalar bilan bildirishnoma guruh.
- PendingIntent: Bildirishnoma bosilganda bajariladigan amal.
Shuningdek, katta rasm (setStyle(new NotificationCompat.BigPictureStyle())), harakat tugmalari (addAction()), ovoz, titrash va boshqa parametrlar qo'shilishi mumkin.