# ctx.notify

Remind someone of the thing they asked you to remember — a to-do at six, a timer that ran out — and tell a friend the moment someone does something for them in your app: a note in their guestbook, a gift. It shows as a number on your app’s icon and on their avatar, a row in their Fave1 notifications, and a push if they turned it on — even with your app closed. While your app is open, on() lets you refresh your own screen the moment one arrives.

no permission · card mode · app mode

## Calls

`ctx.notify`

```ts
await ctx.notify.send({ title, body, at, id, open });  // to them → { id, at }
await ctx.notify.send({ to, kind, from, open });       // to someone else → { id, at, delivered }
await ctx.notify.cancel(id);                           // → true if there was one
await ctx.notify.status();                             // → 'on' · 'ask' · 'off' …
await ctx.notify.request();                            // asks now if nobody has → the status after
await ctx.notify.allow(to, on);                        // marks a friend → true if that id is someone
ctx.notify.on(() => { /* a note for this app just arrived */ });
ctx.notify.off(fn);                                    // stop listening
ctx.notify.opened                                      // the open value, or null
```

| field | What it is | limit |
| --- | --- | --- |
| `title` | The line they read. Plain text | 1–80 characters |
| `body` | A second line, if you need one | 240 |
| `at` | When it shows — a `Date`, a timestamp in milliseconds or an ISO string. Leave it out to send now | a year ahead |
| `id` | Your name for it. The same `id` again replaces it; `cancel(id)` takes it back | 64 · letters, digits, _ . : - |
| `open` | Comes back as `ctx.notify.opened` when they open your app from the row | 200 |

No permission to declare and no new sentence on your store card — the person says yes in Fave1 the first time you send ([below](#ask)). Someone has to be signed in and have added your app. Notes to someone else use words you declare in `manifest.json` ([To someone else](#to-someone)).

## Send one

Call it from the button they pressed — “Remind me”. Without `to` it goes to the person using your app right now, with your own words.

`src/index.ts`

```ts
remindButton.onclick = async () => {
  const r = await ctx.notify.send({
    id: `todo:${todo.id}`,             // yours — the same id again changes this one
    title: todo.title,                  // 'Water the plants'
    body: 'You asked to be reminded.',
    at: todo.due,                       // a Date — leave it out to send now
    open: `todo/${todo.id}`,           // comes back as ctx.notify.opened
  });
  ctx.ui.toast(`Reminder set for ${new Date(r.at).toLocaleString(ctx.locale)}`);
};
```

It resolves with `{ id, at }`: the `id` you gave — or one Fave1 made up if you gave none, so keep it if you’ll want to cancel — and the time it shows, as an ISO string. A rejection means nothing was sent ([Errors](#errors)).

Fave1 shows the title exactly as you sent it, so write it in their language — [ctx.t](https://developers.fave1.com/docs/ctx-locale.md) — when you send.

> **The notification is the extra.** Save the reminder in your own app first. Signed out, switched off, over the limit — `send()` can reject for reasons that aren’t yours to fix, and the to-do they just made should still be there.

## They say yes once

The first `send()` puts Fave1’s own line in your panel: **Let \<your app> send you notifications?** The buttons are Fave1’s, in a frame your code can’t reach. It’s **once per app, not per site** — after a yes, every later `send()` goes straight through, on any site and in app mode.

**Not now** rejects with `cancelled`. In app mode, or on a site that already allowed your app, it’s remembered as a no: `status()` turns `'off'` and `send()` rejects with `denied` until they switch your app back on in Fave1 Settings › Notifications. Anywhere else it holds for this visit, and the next visit asks again. They can switch it off there at any time, too. Don’t ask again yourself and don’t keep your own flag — `status()` always knows, and it never asks anything.

`src/index.ts`

```ts
const SETTINGS = 'https://www.fave1.com/settings/notifications';

const state = await ctx.notify.status();
if (state === 'off') {
  ctx.ui.render([
    ctx.ui.text('Reminders from this app are turned off.'),
    ctx.ui.button('Open Settings', {
      onClick: () => window.open(SETTINGS, '_blank', 'noopener'),
    }),
  ]);
} else if (state === 'on' || state === 'ask') {
  // offer "Remind me" — on 'ask', the first send() asks
}
```

| status() | What it means | Do |
| --- | --- | --- |
| `on` | They said yes | send() |
| `ask` | Nobody has asked yet — the next `send()` will | send() in a tap |
| `off` | They said Not now, or turned your app off in Settings › Notifications | point at Settings |
| `signed-out` | Nobody’s signed in to Fave1 here | skip it |
| `not-installed` | They haven’t added your app — a Try it, or a site’s button | skip it |

> **Ask from their tap.** Call `send()` from the button they pressed — never at launch, when the question answers nothing they did. On a site Fave1 brings the question to the middle of the screen, even if your panel is folded or hidden, and puts your panel back the way it was once they answer. You don’t have to show anything first.

## Schedule, replace, cancel

`at` takes a `Date`, a timestamp or an ISO string, up to a year ahead. Fave1 keeps the schedule: the notification shows at that time whether your app is open or not, on this device or another — no timer of yours has to be running. A time that’s already past sends now.

Give it an `id` and you can change your mind. Sending the same `id` again **replaces** that notification — title, body, time, `open` — so there’s one row, never two; one that was already shown comes back as new. `cancel(id)` removes it whether it’s still waiting or already shown, and resolves `true` if there was one.

`src/index.ts`

```ts
// They moved the to-do to tomorrow — same id, so the old reminder is replaced
await ctx.notify.send({
  id: `todo:${todo.id}`,
  title: todo.title,
  at: todo.due,                       // the new time
  open: `todo/${todo.id}`,
});

// They ticked it off — take the reminder back, waiting or already shown
await ctx.notify.cancel(`todo:${todo.id}`);
```

Build the `id` from your own data — `todo:42` — and you never have to store a receipt.

## To someone else

When someone does something **for another person** in your app — signs their guestbook, sends a gift, invites them over, reacts to their post — tell that person, even if your app isn’t open on their side. Address them by their `ctx.user.id`: the `id` on their [pub row](https://developers.fave1.com/docs/ctx-pub.md), or one they shared. You don’t write the words. Declare each kind once in `manifest.json`, and Fave1 fills it in for the reader, in the reader’s language.

`manifest.json`

```json
{
  "notifications": {
    "guestbook": {
      "title": "{from} left a paw print in your guestbook",
      "many": "{n} new paw prints in your guestbook",
      "anon": "Someone left a paw print in your guestbook"
    },
    "gift": { "audience": "friends", "title": "{from} sent you a gift" }
  }
}
```

`locales/ko.json`

```json
{
  "notify.guestbook.title": "{from}님이 방명록에 발자국을 남겼어요",
  "notify.guestbook.many": "방명록에 새 발자국 {n}개",
  "notify.guestbook.anon": "누군가 방명록에 발자국을 남겼어요"
}
```

`src/index.ts`

```ts
// They just signed row's guestbook — save it first, then tell the owner
const r = await ctx.notify.send({
  to: row.id,              // the owner's ctx.user.id
  kind: 'guestbook',       // declared in manifest.json
  from: myName,            // the sender's display name in your app (up to 24) — fills {from}
  open: 'guestbook',       // comes back as ctx.notify.opened on their side
});
if (!r.delivered) {
  // they turned your notifications off, or muted the sender — that's fine, don't retry
}
```

| kind field | What it is | limit |
| --- | --- | --- |
| `title` | The line they read. `{from}` is the sender’s name | 80 with a 24-character name |
| `body` | A second line, if you need one. `{from}` too | 240 |
| `many` | When unread ones of this kind stack up, one row says this instead. `{n}` is how many notes, not people — the same person twice counts twice, so say “3 new replies”, not “3 people”, unless your app sends a kind once per person. It may use `{from}` too | 80 |
| `anon` | The line when the sender isn’t their friend — no name. Without it, `{from}` becomes “Someone” | 80 · no placeholders |
| `audience` | `"friends"` (the default) — only from people they `allow()`ed. `"anyone"` — from anyone who uses your app, nameless unless they’re a friend | friends · anyone |

Kind names start with a lowercase letter, then lowercase letters, digits and `_`, up to 24 in all, twelve kinds at most. `locales/<lang>.json` keys `notify.<kind>.title` · `.body` · `.many` · `.anon` give each language; English can live in either place, and a language without them falls back to English. The upload checks every line — an unknown placeholder or a line that can run too long stops it. The words that go out are the ones in your **live** version, so a new kind starts working when that version is approved.

**Friends.** Call `allow(id)` when this person makes the friendship — accepts an invite, adds someone — and `allow(id, false)` when they undo it. A friend’s notes show their name and `"friends"` kinds arrive; everyone else’s show your `anon` line, and only for `"anyone"` kinds. That way nobody can put a name or a word in front of a stranger.

**Reactions.** A reaction someone deliberately leaves on the reader’s **own** post — a 🔥 on their punchline, a ♥ on their photo — is something done for them, so it can go: one note per reaction, to the post’s owner only. Reactions come in bursts, so a reaction kind **must** have a `many` line — unread notes of one kind within a day already fold into one row that buzzes at most once an hour, and `many` is what that row says. Only a reaction a person actually tapped: never a total (“you made today’s top 10”, “your post hit 50”), and never news about the post itself instead of something someone did to it.

`manifest.json`

```json
{
  "notifications": {
    "fire": {
      "audience": "anyone",
      "title": "🔥 {from} fired up your punchline",
      "many": "🔥 {n} new fires on your punchline",
      "anon": "🔥 Someone fired up your punchline"
    }
  }
}
```

**It lands unless they said no.** A note reaches anyone who has your app, in their Fave1 notifications and as a number on your icon — unless they turned your app’s notifications off or muted the sender. Sending never asks the reader. Pushes to their phone or desktop only go to browsers where they turned push on. Call `request()` at a moment it obviously helps *them* — right after they share their page or make a friend — so the yes is theirs, not at launch.

`src/index.ts`

```ts
shareButton.onclick = async () => {
  await turnOnSharing();
  if ((await ctx.notify.status()) === 'ask') await ctx.notify.request();
};
```

The reader can tap **Mute** on the row to stop notes from that sender in your app; making them a friend again lifts it. On a grouped row only that sender’s share goes — the row stays for everyone else. `delivered` never says which reason stopped a note.

## When they tap it

Tapping your row marks it read and opens your app — in the panel on the site they’re on, or in app mode from My Fave1. On that launch `ctx.notify.opened` is the `open` string you sent; on every other launch it’s `null`. Read it in `onLaunch` and go straight there. Treat it as input, not proof: the row opens app mode with it in the address, so a link someone made by hand can carry it too — check that the thing it names exists and belongs to this person before you show it.

`src/index.ts`

```ts
Fave1.defineApp({
  onLaunch(ctx) {
    const target = ctx.notify.opened;   // 'todo/42' — null on an ordinary launch
    if (target?.startsWith('todo/')) showTodo(target.slice(5));
    else showHome();
  },
});
```

`open` is yours to shape — a path, an id, a few words. Nobody visits it and Fave1 doesn’t read it. The same row can be tapped on a site or in app mode, so make sure the thing it points to can be shown in both ([ctx.surface](https://developers.fave1.com/docs/concepts.md#which-mode)).

## While your app is open

Fave1 already updates the number on your icon and the notifications list the moment a note arrives — even with your app closed. When your app **is** open and a notification for **your** app lands (a reminder that just became due, or a guestbook · gift · invite from someone else), `on(fn)` runs so you can refresh your own screen — an inbox, a list of invites — without waiting for them to leave and come back.

`src/index.ts`

```ts
function refreshInbox() {
  // re-read pub rows, redraw the invites strip, …
}

ctx.notify.on(refreshInbox);          // same function twice still fires once
// later, if you tear the screen down:
ctx.notify.off(refreshInbox);
```

No arguments, no promise — just call your refresh. It can also fire when the count didn’t move — a second invite of the same kind folds into the unread row that’s already there — so keep the refresh cheap and safe to run twice. Closing the app clears every listener. Notes for other apps never fire yours. In a DEV sandbox `on()` never fires: nothing reaches Fave1 there ([While you build](#dev)).

## What they see

- **A number.** On your app’s icon — in Fave1 on any site and in My Fave1 — and added into the one on their avatar. It stops at 99.
- **A row.** In their Fave1 notifications: your icon and name, the time, your title, and up to two lines of the body. Rows stay for 90 days.
- **Opening your app clears it.** Your app’s share of the number goes when they open your app; tapping one row clears that one. There is no call to set the number — it always matches real notifications.
- **Plain text.** No HTML, no markdown, no links — line breaks and control characters turn into spaces. The only place a row leads is your app.
- **Hidden from the page.** On someone else’s site the list is drawn by Fave1 in its own frame. The page underneath can tell there’s a number, never what your notification says.
- **Inside Fave1 first.** It’s waiting the next time they open Fave1, on any device they’re signed in on. It is never an email.
- **A push, if they turned it on.** On a browser where they switched on push in Settings › Notifications, the same notification also arrives as a system notification — your app’s name as the title, your title and body under it — even with Fave1 closed. Tapping it opens your app with the same `open` value. There’s nothing for you to do, and no way to tell which they have.

## The rules

- **Only what they set themselves.** A reminder they made, a timer they started, a date they picked. If they didn’t ask for this exact notification, don’t send it.
- **No news, no offers, no “come back”.** New features, streak nudges, “you haven’t opened us in a while”, a sale, a tip — none of it goes here.
- **To someone else, only for what was done for them.** One note per action someone took for that person — a note, a gift, an invite, a reply, a reaction they left on that person’s own post (that kind needs a `many` line). Not a total like “your post hit 50” or “you made today’s top 10”, not their friend’s high score, not “3 people are online”, not one action broadcast to many.
- **Nothing private in the title.** It shows on their screen, in a list, wherever they open Fave1. Keep passwords, codes and anything they’d mind a bystander reading out of it — put the detail behind `open`.

> **Watch out:** **Review reads what you send and when.** An app that uses notifications to pull people back, instead of telling them what they asked to be told, gets sent back.

## Limits

| What | Limit | Past it |
| --- | --- | --- |
| `title` | 1 to 80 characters, trimmed | invalid |
| `body` | Up to 240 characters | invalid |
| `open` | Up to 200 characters | invalid |
| `id` | Up to 64 letters, digits and `_ . : -` | invalid |
| `at` | Up to a year ahead | invalid |
| `a day` | 5 per person from your app, counted on the day each one shows (UTC) | rate · scope day |
| `waiting` | 50 scheduled and not shown yet, per person from your app | rate · scope pending |
| `from` | Up to 24 characters, plain text | invalid |
| `sent` | 300 notes to other people a day from one sender in your app (UTC) | rate · scope sent |
| `stacking` | Unread notes of one kind within a day share a row; it buzzes at most once an hour | — |
| `friends` | 1,000 per person in your app | rate · scope friends |

Changing or cancelling one that hasn’t shown yet gives its place in that day back. Five is a ceiling for someone with a busy day, not a number to reach.

## Errors

| Code | What happened | Try |
| --- | --- | --- |
| `cancelled` | They chose Not now on Fave1’s question | nothing |
| `denied` | They said no before, or turned your app off in Settings › Notifications | point at Settings |
| `needs-tap` | Fave1 had to ask and couldn’t show its question on this page — show your panel and `send()` again. Sending to someone else (or `allow()`), it means the person using your app hasn’t let it use their account on this site yet — call something that asks once, like `ctx.storage.get()`, then send again | see left |
| `signed-out` | Nobody’s signed in to Fave1 here | skip it |
| `not-installed` | They haven’t added your app — a Try it, or a site’s button | skip it |
| `invalid` | An empty or too-long title, a body, `open` or `id` past its limit, an `at` more than a year out. To someone else: a missing `to` or `kind`, your own id as `to`, or any of `title` · `body` · `at` · `id` — the text comes from your manifest | fix it |
| `blocked` | Fave1 stopped this app. `status()` may still say `'ask'` then | skip it |
| `unknown-kind` | That `kind` isn’t in your live version’s `"notifications"` | declare it · ship |
| `rate` | That day already has five from your app (`err.scope` `'day'`), fifty are waiting (`'pending'`), this sender hit 300 notes today (`'sent'`), or a thousand friends (`'friends'`) | another day · cancel() |
| `unreachable · timeout · server` | Fave1 didn’t answer | later |

`src/index.ts`

```ts
try {
  await ctx.notify.send({ id: `todo:${todo.id}`, title: todo.title, at: todo.due });
} catch (err) {
  const e = err as Fave1Error;
  if (e.code === 'rate' && e.scope === 'day') {
    ctx.ui.toast('That day already has five reminders — pick another day.');
  } else if (e.code === 'denied') {
    ctx.ui.toast('Reminders are off — see Fave1 Settings › Notifications.');
  }
  // cancelled · signed-out · not-installed — the to-do is saved anyway
}
```

## While you build

> **DEV builds and Test links keep notifications in this browser.** `status()` is `'on'`, `send()` never asks and `cancel()` works — but nothing reaches Fave1: no number, no row, nothing to tap, and `on()` never fires. The question, the number and `ctx.notify.opened` come with the live app.
