Skip to content

About

Android/Termux notifications for OpenCode2 with focus suppression and tap-to-session navigation

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenCode Termux Notify

Android notifications for OpenCode2 running in Termux, with focus suppression and tap-to-session navigation.

  • form / question waiting for input
  • permission request
  • execution finished
  • execution failed or interrupted
  • rate limit / retry notice
  • tapping a notification foregrounds Termux and opens the triggering session
  • swiping dismisses the notification without navigating

No overlay permission or SYSTEM_ALERT_WINDOW is used. This is a local TUI plugin plus two small Android helper jars; it does not require Termux:API.

Why not the built-in notifications plugin?

OpenCode's built-in opencode.notifications calls OpenTUI's attention API, which emits a terminal-level notification. Termux's terminal does not turn that into an Android notification, and it has no way to navigate to an OpenCode session. This plugin is the Android/Termux bridge.

The current Termux:API implementation cannot replace the helpers yet because its notification actions use service PendingIntents, which cannot foreground TermuxActivity without overlay permission. See termux/termux-api#935.

Requirements

  • Termux (com.termux) with the OpenCode2 TUI
  • Android 8 or newer
  • Notification permission enabled for Termux
  • javac, d8, and python3 to build the helpers
  • No Termux:API, companion app, or overlay permission required

Install

Clone directly into the OpenCode plugin directory:

CONFIG="${XDG_CONFIG_HOME:-$HOME/.config}/opencode"
mkdir -p "$CONFIG/plugins"
git clone https://github.com/AmjadHD/opencode-termux-notify.git \
  "$CONFIG/plugins/termux-notify"
cd "$CONFIG/plugins/termux-notify"
./install.sh

install.sh builds the helper jars into ~/.local/share/opencode/bin and makes sure the plugin is visible to OpenCode. If you cloned somewhere else, it creates a symlink under the plugin directory instead of overwriting an existing installation.

Make sure the plugin is enabled in ~/.config/opencode/cli.json:

{
  "plugins": [
    "-opencode.notifications",
    "./plugins/termux-notify"
  ]
}

The -opencode.notifications entry is only needed if the built-in plugin was enabled. Restart the OpenCode TUI after changing the configuration.

How it works

  • tui.ts listens for OpenCode TUI events and calls the TermuxNotify helper through app_process.
  • The helper posts an Android notification as com.termux, using a PendingIntent.getActivity(...) content intent for TermuxActivity.
  • A delete intent to TermuxService writes a swipe marker. The plugin treats a notification that disappears without that marker as a tap.
  • Each notification has a queue entry containing the OpenCode session and the TUI process that posted it. Only that process handles the entry, so two OpenCode instances cannot navigate each other's session list. Entries left by a dead TUI process are cancelled.
  • A small long-lived helper watches active notification IDs every 250 ms and writes an atomic snapshot when they change (with a once-per-second refresh). This avoids starting app_process (which can take seconds) for every poll. It exits shortly after the TUI process that started it. If the watcher is unavailable, the plugin falls back to the regular helper call.
  • New notifications are polled every 500 ms for the first minute, every second for the next ten minutes, and every 2 s afterwards. A tap is normally navigated in about 1-2 s; the one-second grace period is used to distinguish a tap from a swipe marker.
  • Focus is checked through the Android activity task state. With one Termux terminal session, notifications are suppressed while Termux is focused. With more than one session, Termux does not expose which tab is selected, so the plugin suppresses events for the OpenCode session currently on screen and delivers events for other OpenCode sessions. This avoids a finished notification for the session you are reading while still surfacing background agents. Set OPENCODE_TERMUX_NOTIFY_ALLOW_MULTI_SESSION=1 to always deliver with multiple Termux sessions, or OPENCODE_TERMUX_NOTIFY_STRICT_FOCUS=1 to suppress everything while Termux is focused.
  • Finished/failed notifications use run-specific IDs, and helper/plugin locks prevent duplicate posts and navigation when the TUI hot-reloads.

Runtime state:

  • queue: ~/.local/share/opencode/notify-queue/ (also holds the watcher lock and active-notification snapshot)
  • dismiss markers: ~/.local/share/opencode/notify-dismissed/
  • run IDs: ~/.local/share/opencode/notify-runs/
  • per-process Termux session mapping: ~/.local/share/opencode/notify-instances/
  • log: ~/.local/share/opencode/termux-tui-plugin.log

Configuration

The plugin honors OpenCode's attention settings:

{
  "attention": {
    "enabled": true,
    "notifications": true,
    "sound": true
  }
}

OPENCODE_NOTIFY_SOUND=0 disables notification sound. Paths and the app_process binary can be overridden for unusual layouts:

Variable Default
OPENCODE_TERMUX_NOTIFY_DATA $HOME/.local/share/opencode
OPENCODE_TERMUX_NOTIFY_JAR $DATA/bin/termux-notify.jar
OPENCODE_TERMUX_NOTIFY_FOCUS_JAR $DATA/bin/focus-state.jar
OPENCODE_TERMUX_NOTIFY_QUEUE_DIR $DATA/notify-queue
OPENCODE_TERMUX_NOTIFY_DISMISSED_DIR $DATA/notify-dismissed
OPENCODE_TERMUX_NOTIFY_RUN_DIR $DATA/notify-runs
OPENCODE_TERMUX_NOTIFY_LOG $DATA/termux-tui-plugin.log
OPENCODE_TERMUX_NOTIFY_APP_PROCESS /system/bin/app_process
OPENCODE_TERMUX_NOTIFY_STRICT_FOCUS unset; set to 1 to suppress whenever Termux is focused
OPENCODE_TERMUX_NOTIFY_ALLOW_MULTI_SESSION unset; set to 1 to deliver all notifications with multiple Termux sessions
OPENCODE_TERMUX_NOTIFY_TERMUX_SESSION auto-detected Termux session handle override

Build

./helpers/build.sh

Set OUT_DIR to build the jars somewhere other than ~/.local/share/opencode/bin:

OUT_DIR=/tmp/termux-notify-build ./helpers/build.sh

The jars are made read-only because Android 14+ refuses to load writable dex files.

Troubleshooting

  • If notifications are missing, check ~/.local/share/opencode/termux-tui-plugin.log and confirm Termux has notification permission.
  • Confirm the plugin is listed by opencode2 plugin list.
  • If the TUI was hot-reloaded repeatedly and events are logged twice, fully restart the TUI to clear old plugin instances.
  • Test the helper directly:
CLASSPATH="$HOME/.local/share/opencode/bin/termux-notify.jar" \
  /system/bin/app_process /system/bin TermuxNotify active-ids
  • Check Android focus state:
CLASSPATH="$HOME/.local/share/opencode/bin/focus-state.jar" \
  /system/bin/app_process /system/bin FocusState

The output is focused visible top, for example 1 1 1.

Multiple Termux sessions

Each Termux terminal session can run its own OpenCode TUI. Every TUI process loads its own copy of this plugin and only handles notifications that it posted. A notification tap opens the OpenCode session inside the TUI that triggered it.

Termux currently does not expose the selected terminal session to other processes, and its activity intent cannot select a terminal session. If OpenCode is running in more than one Termux session, a notification tap foregrounds Termux's current/last terminal session rather than necessarily the tab that posted the notification. The correct OpenCode session is already focused in its own TUI, so switching to that Termux tab shows it; the plugin cannot switch Termux tabs for you. This needs a Termux app API for selecting a session.

Because the focused Termux tab cannot be queried, suppression uses the OpenCode TUI's current route as a proxy when more than one terminal session is running: it suppresses events for the OpenCode session currently on screen and delivers events for other OpenCode sessions. This prevents a duplicate notification for the session you are actively viewing while keeping notifications for background agents.

The proxy cannot tell when the TUI itself is in a background Termux tab: that TUI still reports its own current session, so a notification for it may be suppressed. If you run separate OpenCode TUIs in separate Termux tabs and need every notification, set OPENCODE_TERMUX_NOTIFY_ALLOW_MULTI_SESSION=1 to restore allow-all behavior. Alternatively, set OPENCODE_TERMUX_NOTIFY_STRICT_FOCUS=1 to suppress every notification while Termux is focused.

Limitations

The helper uses Android private APIs through app_process and reflection. It is tested on Android 15 with Termux and may need adjustment on other Android versions or OEM builds. It is intentionally Termux-specific.

If Termux:API gains an activity content intent and a tap command/trampoline, this plugin can drop both helper jars and the active-notification polling. See the upstream feature request linked above. Selecting an existing Termux terminal session from an intent is a separate Termux app capability.

License

MIT. See LICENSE.

About

Android/Termux notifications for OpenCode2 with focus suppression and tap-to-session navigation

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages