Skip to content

Instantly share code, notes, and snippets.

@pavan5208
Created July 8, 2021 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pavan5208/dc5477f29ae34dab5dc7e8bb28d10d6d to your computer and use it in GitHub Desktop.
Save pavan5208/dc5477f29ae34dab5dc7e8bb28d10d6d to your computer and use it in GitHub Desktop.
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