🏗️Oluşturma | Notification
Android üzerinde bildirim oluşturma
👀 Bildirimlere Bakış
👮♂️ Android 8.0 sonrasında
Notification Channel
zorunluluğu gelmiştir🆔 Channel ID verilmesi zorunlu hale getirilmiştir (eskilerde zorunlu değil)
☝ Bildirimlere tıklandığında uygulamanızı açması önemlidir
📦 Paketleri Dahil Etme
👮♂️ Bildirimler için alttaki paketin
build.gradle
içerisine dahil edilmesi gerekir
implementation "com.android.support:support-compat:28.0.0"
📢 Notification Channel
👨💼 Bildirimlerin kategorilerle yönetildiği yapıdır
👪 Kategorilere göre bildirim şekillerini düzenlemeye yardımcı olur
🚀 Her bildirimin farklı biz özelliği vardır ve ayrıca yönetilir
😏 Kullanıcı sadece kendisini rahatsız eden bildirimleri kapatır ve sizin diğer bildirimleriniz devam eder
@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel() {
// Bildirim kanalını özelliklerini oluşturma
val name = getString(R.string.notification_name)
val description = getString(R.string.notification_description)
val importance = NotificationManager.IMPORTANCE_HIGH
// Bildirim kanalını oluşturma
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
this.description = description
lightColor = Color.BLUE
lockscreenVisibility = Notification.VISIBILITY_PRIVATE
}
// Bildirimi sisteme kaydetme
(getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
.apply {
createNotificationChannel(channel)
}
}
🔔 Bildirim Oluşturma
private fun createNotification(): Notification {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel()
}
return NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.notification_title))
.setStyle(NotificationCompat.BigTextStyle()
.bigText(getString(R.string.notification_text)))
.setPriority(NotificationCompat.PRIORITY_MIN)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setAutoCancel(true)
.build()
}
✨ Uygulamayı Gösteren Bildirim
➕
.setContentIntent(createShowAppPI())
olarakNotificationCompat.Builder
üzerine eklenmelidir
private fun createShowAppPI(): PendingIntent {
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
return PendingIntent.getActivity(
this@TelemetryService,
PI_SHOW_APP,
intent,
PendingIntent.FLAG_UPDATE_CURRENT)
}
private fun createNotification(): Notification {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel()
}
return NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.notification_title))
.setStyle(NotificationCompat.BigTextStyle()
.bigText(getString(R.string.notification_text)))
.setPriority(NotificationCompat.PRIORITY_MIN)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setContentIntent(createShowAppPI()) // Yeni eklenen alan
.setAutoCancel(true)
.build()
}
🚫 Bildirim Üzerinden İptal Etme
➕
.addAction(R.drawable.ic_notification, "Kapat", createCloseServicePI())
olarakNotificationCompat.Builder
üzerine eklenmelidir
private const val REQUEST_STOP = 2
@SuppressLint("NewApi")
private fun createCloseServicePI(): PendingIntent {
val stopSelfIntent = Intent(this, TelemetryService::class.java).apply {
action = ACTION_STOP_SERVICE
}
return when (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
true ->
PendingIntent.getForegroundService(
this,
REQUEST_STOP,
stopSelfIntent,
PendingIntent.FLAG_CANCEL_CURRENT
)
false ->
PendingIntent.getService(
this,
REQUEST_STOP,
stopSelfIntent,
PendingIntent.FLAG_CANCEL_CURRENT
)
}
}
private fun createNotification(): Notification {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel()
}
return NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(getString(R.string.notification_title))
.setStyle(NotificationCompat.BigTextStyle()
.bigText(getString(R.string.notification_text)))
.setPriority(NotificationCompat.PRIORITY_MIN)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setAutoCancel(true)
// Yeni gelen alan 👇
.addAction(R.drawable.ic_notification, "Kapat", createCloseServicePI())
.build()
}
🔗 Faydalı Bağlantılar
🚀 Bu bağlantıların hepsi YEmoji yapısına uygundur
Last updated
Was this helpful?