I wanted to create a notification with heads up feature . Basically it is a alarm feature. And I wanted to show with notification using foreground
. Now workmanager
has the feature to run in foreground using setForeground
. But it's not showing notifications as I implement it as following :
@HiltWorker
class NotificationWorker @AssistedInject constructor(
@Assisted context: Context,
@Assisted workerParams: WorkerParameters,
) : CoroutineWorker(context, workerParams) {
override suspend fun doWork(): Result {
Log.d(TAG, "doWork: notification worker called ")
setForeground(createForegroundInfo())
return Result.success()
}
private fun createForegroundInfo(): ForegroundInfo
{
val msg = "messgae "
val id = 34
val channelId = "show_reminder"
val channelName = "Reminder Notification"
val alarmSound: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
val vibratePattern = longArrayOf(500, 500, 500, 500, 500, 500, 500, 500, 500)
val customView = RemoteViews("packageName", R.layout.alarm_notification)
customView.setTextViewText(R.id.content, msg.trim())
val notificationManager = applicationContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH)
notificationManager.createNotificationChannel(channel)
}
...
val builder = NotificationCompat.Builder(applicationContext,channelId)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentText(msg.trim())
.setStyle(NotificationCompat.BigTextStyle()
.bigText(msg.trim()))
.setSmallIcon(R.drawable.ic_baseline_access_alarm_24)
.setSound(alarmSound)
.setVibrate(vibratePattern)
.setCustomHeadsUpContentView(customView)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
//notificationManager.notify(id, builder.build())
return ForegroundInfo(id,builder.build())
}
}
When calling notificationManager.notify(id, builder.build()) displays the notification but it won't stay long .How to make heads up notification stay long ?