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

@@ -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(),
)
}