How to hide the app name in foreground notification?
Solution 1:
To keep the service active in the foreground you can create your own notification and customize it as you like.
You can use this example code as a base.
lateinit var notificationBuilder: Notification
private fun setNotification(title: String, descr: String) {
val pendingIntent = PendingIntent.getActivity(
this, 0, Intent(
this,
MainActivity::class.java
), 0
)
createNotificationChannel()
notificationBuilder = NotificationCompat.Builder(this, "misc")
.setContentTitle(title)
.setContentText(descr)
.setOnlyAlertOnce(true)
.setSmallIcon(R.drawable.ic_nordic_icon)
.setContentIntent(pendingIntent)
.build()
startForeground(1, notificationBuilder)
}
lateinit var manager: NotificationManager
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val serviceChannel = NotificationChannel(
"misc",
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
)
manager = getSystemService(
NotificationManager::class.java
)
manager.createNotificationChannel(serviceChannel)
}
}
And call it inside your service like:
setNotification(
"My title",
"A notification description"
)
Official doc here.
Update
You can hide the notification using a configuration like:
serviceChannel = new NotificationChannel(
"misc",
"Foreground Service Channel",
NotificationManager.IMPORTANCE_UNSPECIFIED
);
notification = new NotificationCompat.Builder(this, "misc")
.setPriority(Notification.PRIORITY_MIN)
.setContentTitle(title)
.setContentText(descr)
.setContentIntent(pendingIntent)
.build();
Remember to mark your answer as correct if it helped you. Thank you.