> ## Documentation Index
> Fetch the complete documentation index at: https://docs.elata.bio/llms.txt
> Use this file to discover all available pages before exploring further.

# 通知

> 在应用打开期间，通过 Elata 向用户显示提醒。

## 为什么应用不能直接发送通知

你的应用运行在跨源的沙箱 iframe 中。浏览器会在其中屏蔽 `Notification` API：权限始终为 `denied`，`requestPermission()` 也不会弹出提示。

有两种受支持的方式：

| 方式 | 适用条件 | 最适合 |
| - | - | - |
| **由 Elata 代发的提醒**（本页） | Elata 标签页处于打开状态 | “会话完成”、“该休息了” |
| **已安装的应用** | 用户已将你的应用添加到主屏幕 | 在所有页面关闭时提醒 |

***

## 请求提醒

```js theme={null}
window.parent?.postMessage(
  {
    type: 'elata:notify:request',
    requestId: 'optional-id',
    body: 'Focus block complete. Nice work!',
  },
  '*',
);
```

* `body` 为纯文本。Elata 会去除控制字符、合并为单行，并截断至 **140 个字符**。
* 你无法设置标题、图标、点击链接或操作按钮，这些都由 Elata 设置。

Elata 会回复 `{ type: 'elata:notify:state', requestId?, status }`：

| status | 含义 | 建议的界面 |
| - | - | - |
| `shown` | 已显示通知 | 显示提醒已开启 |
| `undecided` | 用户尚未开启；Elata 正在显示“允许提醒？”提示 | 中性的待定状态 |
| `blocked` | 用户已关闭，或系统拒绝了权限 | 停止请求；建议安装应用 |
| `rate_limited` | 一分钟内请求超过 3 次 | 稍后再试 |
| `unsupported` | 该浏览器没有 Notification API | 隐藏该设置 |

***

## Elata 的保证

* **标题由 Elata 撰写。** 始终为 `Elata · <你的应用名称>`，取自你的应用页面，因此应用无法冒充系统、Elata 或其他应用。
* **按应用开启。** 默认关闭。首次请求会显示框架内提示，用户的选择会按应用记住。
* **频率限制。** 每个应用每分钟最多 3 条提醒。
* **仅限前台。** 提醒只会在 Elata 标签页打开时出现。点击提醒会聚焦到 Elata。

***

## 检测是否在 Elata 中

仅在 Elata 内运行时使用桥接，在独立运行或已安装时保留你的直接调用方式：

```js theme={null}
const inStore = window.parent !== window;
if (inStore) {
  window.parent.postMessage({ type: 'elata:notify:request', body }, '*');
} else if (Notification.permission === 'granted') {
  new Notification('My App', { body });
}
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.