Skip to content

Instantly share code, notes, and snippets.

@pavan5208
Created Jul 8, 2021
Embed
What would you like to do?
private fun generateForegroundNotification() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val intentMainLanding = Intent(this, MainActivity::class.java)
val pendingIntent =
PendingIntent.getActivity(this, 0, intentMainLanding, 0)
iconNotification = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
if (mNotificationManager == null) {
mNotificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
assert(mNotificationManager != null)
mNotificationManager?.createNotificationChannelGroup(
NotificationChannelGroup("chats_group", "Chats")
)
val notificationChannel =
NotificationChannel("service_channel", "Service Notifications",
NotificationManager.IMPORTANCE_MIN)
notificationChannel.enableLights(false)
notificationChannel.lockscreenVisibility = Notification.VISIBILITY_SECRET
mNotificationManager?.createNotificationChannel(notificationChannel)
}
val builder = NotificationCompat.Builder(this, "service_channel")
builder.setContentTitle(StringBuilder(resources.getString(R.string.app_name)).append(" service is running").toString())
.setTicker(StringBuilder(resources.getString(R.string.app_name)).append("service is running").toString())
.setContentText("Touch to open") // , swipe down for more options.
.setSmallIcon(R.drawable.ic_alaram)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setWhen(0)
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent)
.setOngoing(true)
if (iconNotification != null) {
builder.setLargeIcon(Bitmap.createScaledBitmap(iconNotification!!, 128, 128, false))
}
builder.color = resources.getColor(R.color.purple_200)
notification = builder.build()
startForeground(mNotificationId, notification)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment