opencode icon indicating copy to clipboard operation
opencode copied to clipboard

[FEATURE]: Notify the iOS app for sessions started from desktop or CLI (Always Notify toggle in app)

Open AlexanderWagnerDev opened this issue 2 weeks ago • 3 comments

Problem

Sessions started from the desktop app or the CLI never send a push notification to the paired iPhone. Only sessions whose prompt came from the iOS app notify (or sessions with metadata.notification.always === true, which the app cannot set).

Why

opencode-mobile-notifications already implements the opt-in:

// src/index.ts
/** The session's own Always Notify callback, when it's on. */
const alwaysURL = (session) => {
  const notification = session && !session.parentID ? notificationOf(session.metadata) : undefined
  return notification?.always === true ? callbackURL(session?.metadata) : undefined
}

The plugin docstring calls this "the app's Always Notify switch" (0.1.2, latest on npm since 2026-10-31), and finish() uses it for runs that were not prompted from the phone:

const url = entry?.url ?? (input ? await alwaysCallback(sessionID, session) : undefined)

The iOS app has no such switch. Its notification settings expose only Allow Notifications and In-App Notices. So a session created/used from desktop or CLI has no notification metadata and can never notify, even though the phone is paired and other sessions push fine.

Expected

Either of these closes the gap:

  1. A per-session Always Notify toggle in the app, plus a global "notify all sessions" default, writing metadata.notification = { always: true, callbackURL } (the plugin then handles renewal via current(url) and 410 cleanup), or
  2. Turning on Allow Notifications sets always: true on new sessions by default.

Workaround (verified on 2.2.5, Windows, plugin 0.1.2)

A small local plugin in ~/.config/opencode/plugins/notify-desktop.ts arms every session as it is created, and re-arms when a phone prompt reveals a newer callback:

import { Plugin } from "@opencode/plugin"

const KEY = "phoneCallback"

export default Plugin.define({
  id: "notify-desktop",
  async setup(ctx) {
    const controller = new AbortController()
    let url = (await ctx.storage.get(KEY).catch(() => undefined)) as string | undefined

    const arm = async (sessionID: string) => {
      if (!url) return
      const session = await ctx.session.get({ sessionID }, { signal: controller.signal }).catch(() => undefined)
      const metadata = (session?.metadata ?? {}) as Record<string, unknown>
      const current = metadata.notification as Record<string, unknown> | undefined
      if (current?.always === true && current.callbackURL === url) return
      await ctx.session
        .update({ sessionID, metadata: { ...metadata, notification: { always: true, callbackURL: url } } } as never)
        .catch(() => undefined)
    }

    const run = async () => {
      for await (const event of ctx.event.subscribe({ signal: controller.signal })) {
        const data = event.data as Record<string, any>
        if (event.type === "session.inbox.enqueued") {
          if (data.item?.type !== "user") continue
          const callback = data.item?.payload?.metadata?.notification?.callbackURL
          if (typeof callback !== "string") continue
          url = callback
          await ctx.storage.set(KEY, callback).catch(() => undefined)
          await arm(data.sessionID)
        } else if (event.type === "session.created") {
          await arm(data.sessionID)
        }
      }
    }
    run().catch(() => undefined)
    return () => controller.abort()
  },
})

A freshly created session now carries:

"metadata": { "notification": { "always": true, "callbackURL": "https://opencode-notify-relay.anomalyco.workers.dev/v2/notify/…" } }

and desktop runs notify.

Cost note

always makes the plugin run session.generate once per finished run to decide whether the user needs to be told, so a global default is not free. A per-session toggle (option 1) is the cheaper design.

Related: #61549 (iOS receives generic pushes, secure pairing never completes) — separate problem, but it also ends in "no useful notification for a session the user did not start from the phone".

AlexanderWagnerDev avatar Nov 14 '26 23:11 AlexanderWagnerDev

Thanks for the detailed write-up and for sharing your workaround plugin.

To restate the request: sessions started from the desktop app or the CLI should be able to push to the paired iPhone. That would come from an Always Notify toggle in the iOS app, per session and/or as a global default, or from Allow Notifications turning it on for new sessions. Your note that a per-session toggle avoids an extra generation on every finished run is useful for the design.

The mobile apps' source is not in this repository, so a person on the team will look at this, and maintainers will consider the request. Until then, your local plugin seems like a reasonable stopgap for anyone else who needs it.

Related issues I found:

  • #61549: iOS receives generic push notifications and secure notification pairing never completes. A separate problem, as you note.
  • #58428: Web Push notifications for paired browsers. A similar wish for notifications from runs on another client, but for the web app.

opencodejr[bot] avatar Nov 14 '26 23:11 opencodejr[bot]

Workaround while this ships: https://gist.github.com/AlexanderWagnerDev/f8f8bc2490359d7c6202896d27ccc8fb

A ~50 line local plugin (~/.config/opencode/plugins/notify-from-desktop.ts) that arms every session on session.inbox.enqueued and session.created with notification: { always: true, callbackURL }. Because this also runs for prompts from web, desktop and CLI, the existing opencode-mobile-notifications plugin then sends its normal encrypted push with title and body — no changes to that plugin needed.

Notes from testing on 2.2.5 (Windows, one shared server for desktop and CLI):

  • Arming on every user message matters, not just on prompts carrying a callbackURL — prompts from the web client have none, so a callback-only check leaves sessions unarmed and silently pushes nothing.
  • The server loads one plugin instance per location and each instance receives every event, so pushing from an execution event without a shared claim produces 3-4 duplicate notifications per run. The gist dedupes with a claim map on globalThis.
  • A stale or mistyped callbackURL fails silently: the relay answers 404 {"error":"not_found"} and nothing arrives. Worth logging non-2xx responses.
  • Only {} (generic "New activity") and a valid {box} are accepted; plaintext {"title","body"} returns 400 {"error":"invalid_request"}.
  • A sealed push that "doesn't arrive" while the generic one does can simply mean the iOS app was in the foreground: banners are suppressed for the open app unless In-App Notices is on.

AlexanderWagnerDev avatar Nov 14 '26 23:11 AlexanderWagnerDev

Server supports push notifications as of 2.4.3, our mobile apps will be shipping notifications soon

Tarquinen avatar Dec 01 '26 17:12 Tarquinen