Open the run's artifact from notifications
Phone and browser notifications that carry an artifact open it full screen in the artifact viewer, with an Open run action and the preview as expanded text; pipelines://artifact deep links route to the viewer.
This commit is contained in:
+50
-26
@@ -10,14 +10,19 @@ import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.Person
|
||||
import net.awan.pipelines_app.MainActivity
|
||||
import net.awan.pipelines_app.R
|
||||
|
||||
data class NotificationIntentSpec(
|
||||
val ids: String,
|
||||
val action: String?,
|
||||
val data: String?,
|
||||
)
|
||||
|
||||
/**
|
||||
* Posts server notifications in the system tray. Each chat or run is one
|
||||
* conversation notification (a MessagingStyle thread with the latest lines and
|
||||
* the unread count), keyed by its server group. Thread notifications share a
|
||||
* conversation notification with the latest artifact preview and unread count,
|
||||
* keyed by its server group. Thread notifications share a
|
||||
* group per namespace, so Android bundles them under a summary ("N chats",
|
||||
* "N runs"). A tap opens the app at the `click` deep link and clears the
|
||||
* thread; a dismissal is reported back so the server clears it everywhere.
|
||||
@@ -111,10 +116,18 @@ class NotificationPresenter(private val context: Context) {
|
||||
.setSmallIcon(R.drawable.ic_stat_pipelines)
|
||||
.setContentTitle(title)
|
||||
.setContentText(notification.message)
|
||||
.setStyle(NotificationCompat.BigTextStyle().bigText(notification.message))
|
||||
.setStyle(NotificationCompat.BigTextStyle().bigText(notification.preview.ifEmpty { notification.message }))
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(openIntent(listOf(notification.id), notification.click, id))
|
||||
.setDeleteIntent(dismissIntent(listOf(notification.id), id))
|
||||
.apply {
|
||||
if (runActionLabel(notification.runClick) != null) addAction(
|
||||
NotificationCompat.Action.Builder(
|
||||
R.drawable.ic_stat_pipelines, "Open run",
|
||||
openIntent(listOf(notification.id), notification.runClick, id + 1),
|
||||
).build(),
|
||||
)
|
||||
}
|
||||
createdMillis(notification.createdAt)?.let { builder.setWhen(it).setShowWhen(true) }
|
||||
applyLegacyPriority(builder, notification.kind, notification.priority)
|
||||
manager.notify(id, builder.build())
|
||||
@@ -139,26 +152,21 @@ class NotificationPresenter(private val context: Context) {
|
||||
.setWhen(latest.whenMillis)
|
||||
.setShowWhen(true)
|
||||
.setContentIntent(openIntent(ids, latest.click, id))
|
||||
.apply { addRunAction(latest, id) }
|
||||
.setDeleteIntent(dismissIntent(ids, id))
|
||||
builder.setStyle(conversationStyle(entries))
|
||||
builder.setStyle(NotificationCompat.BigTextStyle().bigText(threadPreview(latest)))
|
||||
applyLegacyPriority(builder, latest.kind, latest.priority)
|
||||
manager.notify(id, builder.build())
|
||||
return channel
|
||||
}
|
||||
|
||||
/**
|
||||
* The shown lines of a thread, most recent last, as a messaging conversation.
|
||||
* NotificationCompat renders it on every API level the app supports.
|
||||
*/
|
||||
private fun conversationStyle(entries: List<ThreadEntry>): NotificationCompat.MessagingStyle {
|
||||
val style = NotificationCompat.MessagingStyle(Person.Builder().setName("You").build())
|
||||
.setConversationTitle(entries.last().sender)
|
||||
.setGroupConversation(false)
|
||||
for (entry in entries.takeLast(SHOWN_LINES)) {
|
||||
val sender = Person.Builder().setName(entry.sender).build()
|
||||
style.addMessage(NotificationCompat.MessagingStyle.Message(entry.text, entry.whenMillis, sender))
|
||||
}
|
||||
return style
|
||||
private fun NotificationCompat.Builder.addRunAction(entry: ThreadEntry, requestCode: Int): NotificationCompat.Builder {
|
||||
if (runActionLabel(entry.runClick) == null) return this
|
||||
addAction(NotificationCompat.Action.Builder(
|
||||
R.drawable.ic_stat_pipelines, "Open run",
|
||||
openIntent(listOf(entry.id), entry.runClick, requestCode + 1),
|
||||
).build())
|
||||
return this
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -201,16 +209,14 @@ class NotificationPresenter(private val context: Context) {
|
||||
* in the extra), so the server marks the whole thread seen.
|
||||
*/
|
||||
private fun openIntent(ids: List<String>, click: String, requestCode: Int): PendingIntent {
|
||||
val spec = notificationIntent(ids, click)
|
||||
val intent = Intent(context, MainActivity::class.java)
|
||||
.putExtra(EXTRA_NOTIFICATION_ID, spec.ids)
|
||||
.addFlags(
|
||||
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or
|
||||
Intent.FLAG_ACTIVITY_CLEAR_TOP,
|
||||
)
|
||||
.putExtra(EXTRA_NOTIFICATION_ID, ids.joinToString("\n"))
|
||||
// Only the app's own links are routed; Flutter's deep-link handling
|
||||
// turns pipelines://run/<id> and pipelines://chat/<id> into a route.
|
||||
val link = Uri.parse(click)
|
||||
if (link.scheme == "pipelines") intent.setAction(Intent.ACTION_VIEW).setData(link)
|
||||
if (spec.action != null) intent.setAction(spec.action).setData(Uri.parse(spec.data))
|
||||
return PendingIntent.getActivity(
|
||||
context,
|
||||
requestCode,
|
||||
@@ -266,6 +272,27 @@ class NotificationPresenter(private val context: Context) {
|
||||
NotificationCompat.Builder(context, channel)
|
||||
|
||||
companion object {
|
||||
/** Kept pure so notification link construction can be regression-tested. */
|
||||
fun notificationLink(click: String): String = click
|
||||
|
||||
/** Builds the deep-link portion of a tray tap without requiring Android Context. */
|
||||
fun notificationIntent(ids: List<String>, click: String): NotificationIntentSpec {
|
||||
val link = notificationLink(click)
|
||||
val appLink = link.substringBefore(":", "") == "pipelines"
|
||||
return NotificationIntentSpec(
|
||||
ids = ids.joinToString("\n"),
|
||||
action = if (appLink) Intent.ACTION_VIEW else null,
|
||||
data = if (appLink) link else null,
|
||||
)
|
||||
}
|
||||
|
||||
fun runActionLabel(runClick: String): String? =
|
||||
if (runClick.isEmpty()) null else "Open run"
|
||||
|
||||
/** The expanded text for a grouped notification, preferring artifact preview. */
|
||||
fun threadPreview(entry: ThreadEntry): String =
|
||||
entry.preview.ifEmpty { entry.text }
|
||||
|
||||
const val CONNECTION_ID = 1
|
||||
const val CONNECTION_CHANNEL = "pipelines.connection"
|
||||
/** The "sign in again" notice; ids up to RESERVED_IDS are the service's own. */
|
||||
@@ -273,9 +300,6 @@ class NotificationPresenter(private val context: Context) {
|
||||
const val ACCOUNT_CHANNEL = "pipelines.account"
|
||||
/** Holds the server id(s) a tap or dismissal clears, newline-separated. */
|
||||
const val EXTRA_NOTIFICATION_ID = "net.awan.pipelines.notification_id"
|
||||
/** Lines of a thread shown in its conversation; the unread count still counts all. */
|
||||
const val SHOWN_LINES = 5
|
||||
|
||||
private val KIND_LABELS = mapOf(
|
||||
"run_waiting" to "Run waiting for you",
|
||||
"run_failed" to "Run failed",
|
||||
|
||||
@@ -15,6 +15,18 @@ data class ServerNotification(
|
||||
val tags: List<String>,
|
||||
val createdAt: String,
|
||||
val group: String,
|
||||
val artifact: NotificationArtifact? = null,
|
||||
val preview: String = "",
|
||||
val runClick: String = "",
|
||||
)
|
||||
|
||||
data class NotificationArtifact(
|
||||
val runId: Int,
|
||||
val sequence: Int,
|
||||
val path: String,
|
||||
val name: String,
|
||||
val url: String,
|
||||
val mediaType: String,
|
||||
)
|
||||
|
||||
/** The frames the notification service reacts to; everything else is [Ignored]. */
|
||||
@@ -99,9 +111,21 @@ object NotificationProtocol {
|
||||
tags = strings(json.optJSONArray("tags")),
|
||||
createdAt = json.text("created_at"),
|
||||
group = json.text("group").trim(),
|
||||
artifact = json.optJSONObject("artifact")?.let { artifact(it) },
|
||||
preview = json.text("preview"),
|
||||
runClick = json.text("run_click").trim(),
|
||||
)
|
||||
}
|
||||
|
||||
private fun artifact(json: JSONObject): NotificationArtifact? {
|
||||
val run = json.optInt("run_id", 0)
|
||||
val sequence = json.optInt("sequence", -1)
|
||||
val path = json.text("path").trim()
|
||||
if (run <= 0 || sequence < 0 || path.isEmpty()) return null
|
||||
return NotificationArtifact(run, sequence, path, json.text("name"),
|
||||
json.text("url"), json.text("media_type"))
|
||||
}
|
||||
|
||||
/** org.json renders a JSON null as the text "null"; treat it as absent. */
|
||||
private fun JSONObject.text(key: String): String =
|
||||
if (isNull(key)) "" else optString(key)
|
||||
|
||||
+11
-3
@@ -16,6 +16,8 @@ data class ThreadEntry(
|
||||
val text: String,
|
||||
val click: String,
|
||||
val whenMillis: Long,
|
||||
val preview: String = "",
|
||||
val runClick: String = "",
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -108,6 +110,8 @@ class ThreadHistory(
|
||||
.put("text", entry.text)
|
||||
.put("click", entry.click)
|
||||
.put("whenMillis", entry.whenMillis)
|
||||
.put("preview", entry.preview)
|
||||
.put("runClick", entry.runClick)
|
||||
|
||||
private fun entryFrom(json: JSONObject) = ThreadEntry(
|
||||
id = json.optString("id"),
|
||||
@@ -117,6 +121,8 @@ class ThreadHistory(
|
||||
text = json.optString("text"),
|
||||
click = json.optString("click"),
|
||||
whenMillis = json.optLong("whenMillis"),
|
||||
preview = json.optString("preview"),
|
||||
runClick = json.optString("runClick"),
|
||||
)
|
||||
|
||||
companion object {
|
||||
@@ -160,8 +166,10 @@ object NotificationThreads {
|
||||
kind = notification.kind,
|
||||
priority = notification.priority,
|
||||
sender = senderOf(notification.kind, notification.title),
|
||||
text = notification.message.ifEmpty { notification.title },
|
||||
text = notification.preview.ifEmpty { notification.message.ifEmpty { notification.title } },
|
||||
click = notification.click,
|
||||
whenMillis = NotificationPresenter.createdMillis(notification.createdAt) ?: receivedMillis,
|
||||
)
|
||||
whenMillis = NotificationPresenter.createdMillis(notification.createdAt) ?: receivedMillis,
|
||||
preview = notification.preview,
|
||||
runClick = notification.runClick,
|
||||
)
|
||||
}
|
||||
|
||||
+56
@@ -3,10 +3,66 @@ package net.awan.pipelines_app.notify
|
||||
import org.json.JSONObject
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class NotificationProtocolTest {
|
||||
@Test
|
||||
fun parsesArtifactFieldsAndRunClick() {
|
||||
val frame = NotificationProtocol.parse(
|
||||
"""{"type":"notification","notification":{"id":"a","click":"pipelines://artifact/7/2/x.md","run_click":"pipelines://run/7","preview":"hello","artifact":{"run_id":7,"sequence":2,"path":"x.md","name":"x.md","url":"/artifacts/7/2/public/x.md","media_type":"text/markdown"}}}""",
|
||||
null,
|
||||
) as NotificationFrame.Pushed
|
||||
assertEquals("pipelines://run/7", frame.notification.runClick)
|
||||
assertEquals("hello", frame.notification.preview)
|
||||
assertEquals("x.md", frame.notification.artifact?.path)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildsArtifactIntentLinkAndRunActionOnlyWhenPresent() {
|
||||
assertEquals(
|
||||
"pipelines://artifact/7/2/a/b.md",
|
||||
NotificationPresenter.notificationLink("pipelines://artifact/7/2/a/b.md"),
|
||||
)
|
||||
assertEquals("Open run", NotificationPresenter.runActionLabel("pipelines://run/7"))
|
||||
assertEquals(null, NotificationPresenter.runActionLabel(""))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun buildsArtifactIntentWithViewActionDataAndClearedIds() {
|
||||
val intent = NotificationPresenter.notificationIntent(
|
||||
listOf("n-1", "n-2"),
|
||||
"pipelines://artifact/7/2/docs/report.md",
|
||||
)
|
||||
assertEquals("android.intent.action.VIEW", intent.action)
|
||||
assertEquals("pipelines://artifact/7/2/docs/report.md", intent.data)
|
||||
assertEquals("n-1\nn-2", intent.ids)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun nonAppClickDoesNotReceiveViewActionOrData() {
|
||||
val intent = NotificationPresenter.notificationIntent(listOf("n-1"), "")
|
||||
assertNull(intent.action)
|
||||
assertNull(intent.data)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun groupedNotificationUsesLatestArtifactPreviewAsExpandedText() {
|
||||
val entry = ThreadEntry(
|
||||
id = "n-1",
|
||||
kind = "run_succeeded",
|
||||
priority = 3,
|
||||
sender = "Run 7",
|
||||
text = "Fallback message",
|
||||
click = "pipelines://artifact/7/2/report.md",
|
||||
whenMillis = 1L,
|
||||
preview = "Rendered artifact preview…",
|
||||
)
|
||||
assertEquals("Rendered artifact preview…", NotificationPresenter.threadPreview(entry))
|
||||
assertEquals("Fallback message", NotificationPresenter.threadPreview(entry.copy(preview = "")))
|
||||
}
|
||||
|
||||
private val pushed = """
|
||||
{"type":"notification","id":"n-7","kind":"run_failed","title":"Run 12 failed",
|
||||
"message":"Step build failed","click":"pipelines://run/12","priority":4,
|
||||
|
||||
Reference in New Issue
Block a user