-
-
Save pavan5208/dc5477f29ae34dab5dc7e8bb28d10d6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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