fix(#114): reduce intrusion of background service notification
This commit is contained in:
1 parent
b1c628ee24
commit
a7d730cae5
1 file changed
+24
-12
@@ -41,7 +41,8 @@ import kotlinx.coroutines.launch
|
|||||||
*/
|
*/
|
||||||
class ChatNotificationService : Service() {
|
class ChatNotificationService : Service() {
|
||||||
companion object {
|
companion object {
|
||||||
private const val CHANNEL_ID = "hermes_chat"
|
private const val SERVICE_CHANNEL_ID = "hermes_service"
|
||||||
|
private const val CHAT_CHANNEL_ID = "hermes_chat"
|
||||||
private const val NOTIFICATION_ID = 1
|
private const val NOTIFICATION_ID = 1
|
||||||
private const val PENDING_NOTIFICATION_ID = 2
|
private const val PENDING_NOTIFICATION_ID = 2
|
||||||
|
|
||||||
@@ -57,8 +58,8 @@ class ChatNotificationService : Service() {
|
|||||||
|
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
createNotificationChannel()
|
createNotificationChannels()
|
||||||
startForeground(NOTIFICATION_ID, buildForegroundNotification("Hermes connected"))
|
startForeground(NOTIFICATION_ID, buildForegroundNotification("Waiting for Hermes replies"))
|
||||||
startEventCollection()
|
startEventCollection()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +92,7 @@ class ChatNotificationService : Service() {
|
|||||||
private fun showReplyNotification(text: String) {
|
private fun showReplyNotification(text: String) {
|
||||||
val notification =
|
val notification =
|
||||||
NotificationCompat
|
NotificationCompat
|
||||||
.Builder(this, CHANNEL_ID)
|
.Builder(this, CHAT_CHANNEL_ID)
|
||||||
.setSmallIcon(R.drawable.ic_notification)
|
.setSmallIcon(R.drawable.ic_notification)
|
||||||
.setContentTitle("Hermes")
|
.setContentTitle("Hermes")
|
||||||
.setContentText(text)
|
.setContentText(text)
|
||||||
@@ -115,26 +116,37 @@ class ChatNotificationService : Service() {
|
|||||||
|
|
||||||
private fun buildForegroundNotification(text: String): Notification =
|
private fun buildForegroundNotification(text: String): Notification =
|
||||||
NotificationCompat
|
NotificationCompat
|
||||||
.Builder(this, CHANNEL_ID)
|
.Builder(this, SERVICE_CHANNEL_ID)
|
||||||
.setSmallIcon(R.drawable.ic_notification)
|
.setSmallIcon(R.drawable.ic_notification)
|
||||||
.setContentTitle("Hermes")
|
.setContentTitle("Hermes")
|
||||||
.setContentText(text)
|
.setContentText(text)
|
||||||
.setPriority(NotificationCompat.PRIORITY_LOW)
|
.setPriority(NotificationCompat.PRIORITY_MIN)
|
||||||
.setOngoing(true)
|
.setOngoing(true)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
private fun createNotificationChannel() {
|
private fun createNotificationChannels() {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
val channel =
|
val serviceChannel =
|
||||||
NotificationChannel(
|
NotificationChannel(
|
||||||
CHANNEL_ID,
|
SERVICE_CHANNEL_ID,
|
||||||
"Hermes Chat",
|
"Hermes Background Service",
|
||||||
|
NotificationManager.IMPORTANCE_MIN,
|
||||||
|
).apply {
|
||||||
|
description = "Keeps connection alive in the background"
|
||||||
|
}
|
||||||
|
|
||||||
|
val chatChannel =
|
||||||
|
NotificationChannel(
|
||||||
|
CHAT_CHANNEL_ID,
|
||||||
|
"Hermes Chat Replies",
|
||||||
NotificationManager.IMPORTANCE_HIGH,
|
NotificationManager.IMPORTANCE_HIGH,
|
||||||
).apply {
|
).apply {
|
||||||
description = "Notifications for new Hermes replies"
|
description = "Alerts for new replies from Hermes"
|
||||||
}
|
}
|
||||||
|
|
||||||
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||||
manager.createNotificationChannel(channel)
|
manager.createNotificationChannel(serviceChannel)
|
||||||
|
manager.createNotificationChannel(chatChannel)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user