Scheduled-result notifications: first lines, a lock-screen version, Open / Run again
CI / check (push) Canceled after 0s

From the push's `notification` (server fd0a386): the task's name and the result's first lines
in BigTextStyle for an unlocked phone, setPublicVersion with the task's name and Finished /
Failed for the lock screen, and the actions with the server's labels in the owner's language.
Run again calls POST /schedules/{id}/run and needs the phone unlocked
(setAuthenticationRequired); Pause stays as it was. An older server's payload (no
`notification`) keeps the app's fixed sentences. ScheduleContractTest reads
push_schedule.json and push_schedule_failed.json.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
HomerandClaude Opus 5.5 committed 2026-11-10 13:45:12 +00:00
1 parent ad5c6ca12b
commit cd88c3e712
15 files changed
+119 -26

No files matched your search

+5
View File
@@ -21,6 +21,11 @@ is in alpha and releases come off `main`.
stops it too.
- **A scheduled task that watches a web page** says so on its page (*Only when this page
changes*, and the address), and a run that found the page unchanged is shown as *Unchanged*.
- **A scheduled task's notification shows its result.** Unlocked, it carries the task's name
and the first lines of what it found (pull it open for more); on the lock screen only the
task's name and *Finished* or *Failed*. **Run again** starts the task again from the
notification, once the phone is unlocked. Needs an instance that sends these; an older one
keeps the fixed sentence.
- **A message waiting behind a reply says how many are ahead of it** and for how long it has
waited, where the instance tells.
@@ -557,6 +557,12 @@ data class PushPayload(
val status: String? = null,
val visibility: String? = null,
val actions: List<String> = emptyList(),
/**
* The run's notification as the server wrote it (newer servers; the payload is encrypted end
* to end): the task's name and the result's first lines, the lock-screen version, and the
* actions' labels in the owner's language. Null from an older server: the fixed sentence then.
*/
val notification: RunNotification? = null,
)
// --- The activity panel (`GET /chats/{chat}/activity`) -------------------------------------
@@ -134,10 +134,11 @@ object Notifications {
/**
* A scheduled run's result or failure, on its own channel, built from the server's payload
* alone ([ScheduledNotice]): the payload carries ids and one of the server's own fixed
* sentences, never the result. Unlocked: the instance's name and that sentence. On the lock
* screen the public version: the app's name and "a scheduled task has a result" (or failed).
* A tap and Open go to the run; Pause is there when the server offers it.
* alone ([ScheduledNotice]). Unlocked: the task's name and the result's first lines, as the
* server cut them (BigTextStyle opens them). On the lock screen only the public version: the
* task's name and Finished / Failed (the owner's choice for beta.9; an older server sends no
* words, and the app's name and a fixed sentence stand in). A tap and Open go to the run;
* Run again starts it again, once the phone is unlocked; Pause is there when offered.
*/
private fun scheduled(context: Context, instance: String, payload: PushPayload) {
val notice = ScheduledNotice.from(payload) ?: return
@@ -146,13 +147,14 @@ object Notifications {
val open = opener(context, instance, notice.id, chatId = null, scheduleId = notice.scheduleId, runId = notice.runId)
val public = NotificationCompat.Builder(context, channel)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(fixed)
.setContentTitle(notice.publicTitle.ifBlank { context.getString(R.string.app_name) })
.setContentText(notice.publicBody.ifBlank { fixed })
.build()
val body = payload.body.ifBlank { fixed }
val title = notice.title.ifBlank { payload.title.ifBlank { context.getString(R.string.scheduled) } }
val body = notice.body.ifBlank { payload.body.ifBlank { fixed } }
val builder = NotificationCompat.Builder(context, channel)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(payload.title.ifBlank { context.getString(R.string.scheduled) })
.setContentTitle(title)
.setContentText(body)
.setStyle(NotificationCompat.BigTextStyle().bigText(body))
.setAutoCancel(true)
@@ -160,8 +162,15 @@ object Notifications {
.setCategory(if (notice.failed) Notification.CATEGORY_ERROR else Notification.CATEGORY_STATUS)
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
.setPublicVersion(public)
if (notice.open) builder.addAction(0, context.getString(R.string.viewer_open), open)
if (notice.pause) builder.addAction(0, context.getString(R.string.schedule_pause), pause(context, instance, notice.scheduleId, notice.id))
if (notice.open) builder.addAction(0, notice.label("open", context.getString(R.string.viewer_open)), open)
if (notice.runAgain) {
val again = scheduleAction(context, instance, notice.scheduleId, notice.id, ScheduleActionReceiver.RUN_AGAIN)
builder.addAction(unlocked(notice.label("run_again", context.getString(R.string.schedule_run_again)), again))
}
if (notice.pause) {
val pause = scheduleAction(context, instance, notice.scheduleId, notice.id, ScheduleActionReceiver.PAUSE)
builder.addAction(0, notice.label("pause", context.getString(R.string.schedule_pause)), pause)
}
NotificationManagerCompat.from(context).notify(notice.id, builder.build())
}
@@ -182,15 +191,17 @@ object Notifications {
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)
private fun pause(context: Context, instance: String, schedule: String, notification: Int): PendingIntent = PendingIntent.getBroadcast(
context,
("pause$schedule").hashCode(),
Intent(context, ScheduleActionReceiver::class.java)
.putExtra("instance", instance)
.putExtra("schedule", schedule)
.putExtra("notification", notification),
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)
private fun scheduleAction(context: Context, instance: String, schedule: String, notification: Int, action: String): PendingIntent =
PendingIntent.getBroadcast(
context,
(action + schedule).hashCode(),
Intent(context, ScheduleActionReceiver::class.java)
.putExtra("instance", instance)
.putExtra("schedule", schedule)
.putExtra("action", action)
.putExtra("notification", notification),
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)
private fun answer(context: Context, instance: String, chat: String, card: String, verdict: String, notification: Int): PendingIntent =
PendingIntent.getBroadcast(
@@ -229,16 +240,20 @@ class CardActionReceiver : BroadcastReceiver() {
}
}
/** A scheduled task's Pause, from its notification. */
/** A scheduled task's Pause or Run again, from its notification. */
class ScheduleActionReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val pending = goAsync()
val instance = intent.getStringExtra("instance") ?: return pending.finish()
val schedule = intent.getStringExtra("schedule") ?: return pending.finish()
val action = intent.getStringExtra("action") ?: PAUSE
background.launch {
try {
Push.instance(context, instance)?.let { found ->
runCatching { context.lembas.instances.clientFor(found).pauseSchedule(schedule, true) }
val client = context.lembas.instances.clientFor(found)
// A failure (gone, busy) is the server's to say on the run page; the
// notification goes either way, and a new run sends its own.
runCatching { if (action == RUN_AGAIN) client.runScheduleNow(schedule) else client.pauseSchedule(schedule, true) }
}
NotificationManagerCompat.from(context).cancel(intent.getIntExtra("notification", 0))
} finally {
@@ -246,4 +261,9 @@ class ScheduleActionReceiver : BroadcastReceiver() {
}
}
}
companion object {
const val PAUSE = "pause"
const val RUN_AGAIN = "run_again"
}
}
@@ -5,8 +5,11 @@ import eu.llembas.android.api.PushPayload
/**
* What a scheduled run's push asks the app to show, read from the server's payload alone
* (services/schedule/notify.py `payload`): the schedule and the run it is about, whether it
* failed, and the actions the server offers. A payload without ids falls back to its `url`
* (`/scheduled/{schedule}?run={run}`), as a browser would open it.
* failed, the actions the server offers, and -- from a server that sends its `notification` --
* the words: [title] and [body] (the result's first lines) for an unlocked phone, [publicTitle]
* and [publicBody] (the task's name, Finished / Failed) for the lock screen, and the actions'
* [labels]. A payload without ids falls back to its `url` (`/scheduled/{schedule}?run={run}`),
* as a browser would open it.
*/
data class ScheduledNotice(
val scheduleId: String,
@@ -16,10 +19,20 @@ data class ScheduledNotice(
val askPause: Boolean,
val open: Boolean,
val pause: Boolean,
val runAgain: Boolean = false,
/** Empty when the server sent no `notification`: the app's fixed sentences are used then. */
val title: String = "",
val body: String = "",
val publicTitle: String = "",
val publicBody: String = "",
val labels: Map<String, String> = emptyMap(),
) {
/** One notification per run: a later push about the same run replaces it. */
val id: Int get() = "scheduled:${runId ?: scheduleId}".hashCode()
/** The server's label for [action] in the owner's language, or [fallback]. */
fun label(action: String, fallback: String): String = labels[action]?.takeIf { it.isNotBlank() } ?: fallback
companion object {
const val KIND_RESULT = "schedule"
const val KIND_FAILED = "schedule_failed"
@@ -30,9 +43,13 @@ data class ScheduledNotice(
val schedule = payload.scheduleId?.takeIf { it.isNotBlank() } ?: urlSchedule ?: return null
val run = payload.runId?.takeIf { it.isNotBlank() } ?: urlRun
val failed = payload.kind == KIND_FAILED
val askPause = payload.ask == "pause"
// An older payload named no actions: Open always, Pause for a failure.
val actions = payload.actions.ifEmpty { if (failed) listOf("open", "pause") else listOf("open") }
val words = payload.notification
val ask = words?.ask?.takeIf { it.isNotBlank() } ?: payload.ask
val askPause = ask == "pause"
// The notification's own actions first; an older payload named none: Open always,
// Pause for a failure.
val actions = words?.actions?.takeIf { it.isNotEmpty() }
?: payload.actions.ifEmpty { if (failed) listOf("open", "pause") else listOf("open") }
return ScheduledNotice(
scheduleId = schedule,
runId = run,
@@ -40,6 +57,12 @@ data class ScheduledNotice(
askPause = askPause,
open = "open" in actions,
pause = "pause" in actions || askPause,
runAgain = "run_again" in actions,
title = words?.title.orEmpty(),
body = words?.body.orEmpty(),
publicTitle = words?.public?.title.orEmpty(),
publicBody = words?.public?.body.orEmpty(),
labels = words?.labels.orEmpty(),
)
}
+1
View File
@@ -218,6 +218,7 @@
<string name="channel_scheduled_failures">Selhání naplánovaných úloh</string>
<string name="scheduled_result_public">Naplánovaná úloha má výsledek.</string>
<string name="scheduled_failed_public">Naplánovaná úloha selhala.</string>
<string name="schedule_run_again">Spustit znovu</string>
<string name="card_approval">Povolit to?</string>
<string name="card_allow_once">Povolit jednou</string>
<string name="card_allow_always">Toto vždy povolit</string>
+1
View File
@@ -218,6 +218,7 @@
<string name="channel_scheduled_failures">Geplante Fehler</string>
<string name="scheduled_result_public">Eine geplante Aufgabe hat ein Ergebnis.</string>
<string name="scheduled_failed_public">Eine geplante Aufgabe ist fehlgeschlagen.</string>
<string name="schedule_run_again">Erneut ausführen</string>
<string name="card_approval">Das erlauben?</string>
<string name="card_allow_once">Einmal erlauben</string>
<string name="card_allow_always">Dies immer erlauben</string>
+1
View File
@@ -218,6 +218,7 @@
<string name="channel_scheduled_failures">Fallos programados</string>
<string name="scheduled_result_public">Una tarea programada tiene un resultado.</string>
<string name="scheduled_failed_public">Una tarea programada falló.</string>
<string name="schedule_run_again">Ejecutar de nuevo</string>
<string name="card_approval">¿Permitir esto?</string>
<string name="card_allow_once">Permitir una vez</string>
<string name="card_allow_always">Permitir siempre esto</string>
+1
View File
@@ -218,6 +218,7 @@
<string name="channel_scheduled_failures">Échecs planifiés</string>
<string name="scheduled_result_public">Une tâche planifiée a un résultat.</string>
<string name="scheduled_failed_public">Une tâche planifiée a échoué.</string>
<string name="schedule_run_again">Relancer</string>
<string name="card_approval">Autoriser ceci ?</string>
<string name="card_allow_once">Autoriser une fois</string>
<string name="card_allow_always">Toujours autoriser ceci</string>
+1
View File
@@ -218,6 +218,7 @@
<string name="channel_scheduled_failures">Ütemezett hibák</string>
<string name="scheduled_result_public">Egy ütemezett feladatnak eredménye van.</string>
<string name="scheduled_failed_public">Egy ütemezett feladat sikertelen volt.</string>
<string name="schedule_run_again">Futtatás újra</string>
<string name="card_approval">Engedélyezzük ezt?</string>
<string name="card_allow_once">Engedélyezés egyszer</string>
<string name="card_allow_always">Ennek mindig engedélyezése</string>
+1
View File
@@ -218,6 +218,7 @@
<string name="channel_scheduled_failures">Errori pianificati</string>
<string name="scheduled_result_public">Un’attività pianificata ha un risultato.</string>
<string name="scheduled_failed_public">Un’attività pianificata non è riuscita.</string>
<string name="schedule_run_again">Esegui di nuovo</string>
<string name="card_approval">Consentire questo?</string>
<string name="card_allow_once">Consenti una volta</string>
<string name="card_allow_always">Consenti sempre questo</string>
+1
View File
@@ -218,6 +218,7 @@
<string name="channel_scheduled_failures">Błędy zaplanowanych zadań</string>
<string name="scheduled_result_public">Zaplanowane zadanie ma wynik.</string>
<string name="scheduled_failed_public">Zaplanowane zadanie się nie powiodło.</string>
<string name="schedule_run_again">Uruchom ponownie</string>
<string name="card_approval">Zezwolić na to?</string>
<string name="card_allow_once">Zezwól raz</string>
<string name="card_allow_always">Zawsze na to zezwalaj</string>
+1
View File
@@ -218,6 +218,7 @@
<string name="channel_scheduled_failures">Zlyhania naplánovaných úloh</string>
<string name="scheduled_result_public">Naplánovaná úloha má výsledok.</string>
<string name="scheduled_failed_public">Naplánovaná úloha zlyhala.</string>
<string name="schedule_run_again">Spustiť znova</string>
<string name="card_approval">Povoliť to?</string>
<string name="card_allow_once">Povoliť raz</string>
<string name="card_allow_always">Toto vždy povoliť</string>
+1
View File
@@ -219,6 +219,7 @@
<string name="channel_scheduled_failures">Scheduled failures</string>
<string name="scheduled_result_public">A scheduled task has a result.</string>
<string name="scheduled_failed_public">A scheduled task failed.</string>
<string name="schedule_run_again">Run again</string>
<string name="card_approval">Allow this?</string>
<string name="card_allow_once">Allow once</string>
<string name="card_allow_always">Always allow this</string>
@@ -209,6 +209,35 @@ class ScheduleContractTest {
assertTrue(notice.id != notice.copy(runId = "run3").id)
}
@Test
fun theServersNotificationWordsAndRunAgain() {
// `push_schedule.json`: the task's name and first lines unlocked, Finished on the lock
// screen, Open and Run again with the owner's labels.
val done = ScheduledNotice.from(read("push_schedule", PushPayload.serializer()))!!
assertEquals("Waybread check", done.title)
assertEquals("All the waybread is wrapped.", done.body)
assertEquals("Waybread check", done.publicTitle)
assertEquals("Finished", done.publicBody)
assertTrue(done.open)
assertTrue(done.runAgain)
assertFalse(done.pause)
assertEquals("Run again", done.label("run_again", "x"))
assertEquals("x", done.label("pause", "x"))
// `push_schedule_failed.json`: three failures ask about Pause, beside Open and Run again.
val failed = ScheduledNotice.from(read("push_schedule_failed", PushPayload.serializer()))!!
assertTrue(failed.failed)
assertTrue(failed.askPause)
assertTrue(failed.pause)
assertTrue(failed.runAgain)
assertEquals("Failed", failed.publicBody)
assertEquals("run3", failed.runId)
// The run's own copy (the schedules frame) says the same.
val run = read("schedule_run", RunResult.serializer()).run
assertEquals(listOf("open", "run_again"), run.notification!!.actions)
}
@Test
fun aPushWithOnlyItsUrlAndOneThatIsNotScheduled() {
val bare = PushPayload(kind = "schedule", url = "/scheduled/sched1?run=run1")
+1
View File
@@ -235,6 +235,7 @@ STRINGS: dict[str, str] = {
"channel_scheduled_failures": "Scheduled failures",
"scheduled_result_public": "A scheduled task has a result.",
"scheduled_failed_public": "A scheduled task failed.",
"schedule_run_again": "Run again",
# Cards
"card_approval": "Allow this?",
"card_allow_once": "Allow once",