From e8feb7c43189ea9d2d73d552a21ba5b1f5e6e724 Mon Sep 17 00:00:00 2001 From: chen08209 Date: Wed, 1 May 2024 23:29:32 +0800 Subject: [PATCH] Set Android notification low importance --- .../com/follow/clash/services/FlClashVpnService.kt | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/android/app/src/main/kotlin/com/follow/clash/services/FlClashVpnService.kt b/android/app/src/main/kotlin/com/follow/clash/services/FlClashVpnService.kt index 444a3c03..2237f072 100644 --- a/android/app/src/main/kotlin/com/follow/clash/services/FlClashVpnService.kt +++ b/android/app/src/main/kotlin/com/follow/clash/services/FlClashVpnService.kt @@ -25,7 +25,8 @@ class FlClashVpnService : VpnService() { private val CHANNEL = "FlClash" - var fd: Int? = null; + var fd: Int? = null + private val notificationId: Int = 1 private val passList = listOf( "*zhihu.com", @@ -100,11 +101,12 @@ class FlClashVpnService : VpnService() { fun startForeground(title: String, content: String) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = - NotificationChannel(CHANNEL, "FlClash", NotificationManager.IMPORTANCE_DEFAULT) + NotificationChannel(CHANNEL, "FlClash", NotificationManager.IMPORTANCE_LOW) val manager = getSystemService(NotificationManager::class.java) manager.createNotificationChannel(channel) val intent = Intent(this, MainActivity::class.java) + val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { PendingIntent.getActivity( this, @@ -120,7 +122,9 @@ class FlClashVpnService : VpnService() { PendingIntent.FLAG_UPDATE_CURRENT ) } + val icon = IconCompat.createWithResource(this, this.applicationInfo.icon) + val notification = with(NotificationCompat.Builder(this, CHANNEL)) { setSmallIcon(icon) setContentTitle(title) @@ -132,12 +136,13 @@ class FlClashVpnService : VpnService() { build() } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { - startForeground(1, notification, FOREGROUND_SERVICE_TYPE_SPECIAL_USE) + startForeground(notificationId, notification, FOREGROUND_SERVICE_TYPE_SPECIAL_USE) } else { - startForeground(1, notification) + startForeground(notificationId, notification) } } } + private fun stopForeground() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { stopForeground(Service.STOP_FOREGROUND_REMOVE)