Skip to content

Commit b9a5985

Browse files
committed
perf: notification title/text/addAction
1 parent 227bd52 commit b9a5985

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

app/src/main/kotlin/li/songe/gkd/debug/HttpService.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class HttpService : Service(), OnCreate, OnDestroy {
9393
}
9494
StopServiceReceiver.autoRegister(this)
9595
onCreated {
96-
httpNotif.notifyService(this)
96+
httpNotif.copy(text = "端口-${storeFlow.value.httpServerPort}").notifyService(this)
9797
scope.launchTry(Dispatchers.IO) {
9898
httpServerPortFlow.collect { port ->
9999
httpServerFlow.value?.stop()
@@ -112,9 +112,9 @@ class HttpService : Service(), OnCreate, OnDestroy {
112112
}
113113
if (httpServerFlow.value == null) {
114114
stopSelf()
115-
return@collect
115+
} else {
116+
httpNotif.copy(text = "端口-$port").notifyService(this@HttpService)
116117
}
117-
httpNotif.copy(text = "HTTP服务-$port").notifyService(this@HttpService)
118118
}
119119
}
120120
}

app/src/main/kotlin/li/songe/gkd/notif/Notif.kt

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ data class Notif(
2727
val channel: NotifChannel = NotifChannel.Default,
2828
val id: Int,
2929
val smallIcon: Int = SafeR.ic_status,
30-
val title: String = META.appName,
31-
val text: String,
30+
val title: String,
31+
val text: String? = null,
3232
val ongoing: Boolean,
3333
val autoCancel: Boolean,
3434
val uri: String? = null,
@@ -45,28 +45,29 @@ data class Notif(
4545
},
4646
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
4747
)
48-
val deleteIntent = stopService?.let {
49-
PendingIntent.getBroadcast(
48+
val notification = NotificationCompat.Builder(app, channel.id)
49+
.setSmallIcon(smallIcon)
50+
.setContentTitle(title)
51+
.setContentText(text)
52+
.setContentIntent(contextIntent)
53+
.setOngoing(ongoing)
54+
.setAutoCancel(autoCancel)
55+
if (stopService != null) {
56+
val deleteIntent = PendingIntent.getBroadcast(
5057
app,
5158
0,
5259
Intent().apply {
5360
action = StopServiceReceiver.STOP_ACTION
54-
putExtra(StopServiceReceiver.STOP_ACTION, it.componentName.className)
61+
putExtra(StopServiceReceiver.STOP_ACTION, stopService.componentName.className)
5562
setPackage(META.appId)
5663
},
5764
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
5865
)
66+
notification
67+
.setDeleteIntent(deleteIntent)
68+
.addAction(0, "停止", deleteIntent)
5969
}
60-
val notification = NotificationCompat.Builder(app, channel.id)
61-
.setSmallIcon(smallIcon)
62-
.setContentTitle(title)
63-
.setContentText(text)
64-
.setContentIntent(contextIntent)
65-
.setDeleteIntent(deleteIntent)
66-
.setOngoing(ongoing)
67-
.setAutoCancel(autoCancel)
68-
.build()
69-
return notification
70+
return notification.build()
7071
}
7172

7273
fun notifySelf() {
@@ -91,14 +92,16 @@ data class Notif(
9192

9293
val abNotif = Notif(
9394
id = 100,
95+
title = "GKD",
9496
text = "无障碍正在运行",
9597
ongoing = true,
9698
autoCancel = false,
9799
)
98100

99101
val screenshotNotif = Notif(
100102
id = 101,
101-
text = "截屏服务正在运行",
103+
title = "截屏服务正在运行",
104+
text = "保存快照时截取屏幕",
102105
ongoing = true,
103106
autoCancel = false,
104107
uri = "gkd://page/1",
@@ -107,7 +110,8 @@ val screenshotNotif = Notif(
107110

108111
val floatingNotif = Notif(
109112
id = 102,
110-
text = "悬浮按钮正在显示",
113+
title = "悬浮窗服务正在运行",
114+
text = "点击按钮捕获快照",
111115
ongoing = true,
112116
autoCancel = false,
113117
uri = "gkd://page/1",
@@ -116,7 +120,7 @@ val floatingNotif = Notif(
116120

117121
val httpNotif = Notif(
118122
id = 103,
119-
text = "HTTP服务正在运行",
123+
title = "HTTP服务正在运行",
120124
ongoing = true,
121125
autoCancel = false,
122126
uri = "gkd://page/1",
@@ -125,15 +129,16 @@ val httpNotif = Notif(
125129

126130
val snapshotActionNotif = Notif(
127131
id = 105,
128-
text = "快照服务正在运行",
132+
title = "快照服务正在运行",
133+
text = "捕获快照完成后自动关闭",
129134
ongoing = true,
130135
autoCancel = false,
131136
)
132137

133138
val snapshotNotif = Notif(
134139
channel = NotifChannel.Snapshot,
135140
id = 104,
136-
text = "快照已保存至记录",
141+
title = "快照已保存",
137142
ongoing = false,
138143
autoCancel = true,
139144
uri = "gkd://page/2",

0 commit comments

Comments
 (0)