# NotificationManager：通知未显示，缺少频道。频道 'my_channel_id' 未创建。

- **ID:** `android/notification-channel-missing`
- **领域:** android
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

在 Android 8.0+（API 26+）上发布通知前未创建通知频道，而频道是强制性的。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Android 8.0 (API 26) | active | — | — |
| Android 9 (API 28) | active | — | — |
| Android 10 (API 29) | active | — | — |
| Android 11 (API 30) | active | — | — |
| Android 12 (API 31) | active | — | — |

## 解决方案

1. ```
   Create channel before posting notification: `if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel("my_channel_id", "My Channel", NotificationManager.IMPORTANCE_DEFAULT); notificationManager.createNotificationChannel(channel) }`
   ```
2. ```
   Use `NotificationChannel` with a unique ID and call `createNotificationChannel()` in Application.onCreate() to ensure channel exists before any notification.
   ```
3. ```
   Check if channel exists before posting: `val channel = notificationManager.getNotificationChannel("my_channel_id"); if (channel == null) { createChannel() }`
   ```

## 无效尝试

- **Set targetSdkVersion below 26 to bypass channel requirement** — Google Play requires targetSdkVersion 31+ for new apps; also, notifications still fail on API 26+ devices. (90% 失败率)
- **Use NotificationCompat.Builder without channel ID** — NotificationCompat.Builder still requires a valid channel ID on API 26+; omitting it causes the same error. (85% 失败率)
- **Post notification from a Service instead of Activity** — Service context doesn't bypass channel requirement; channel creation is still needed. (95% 失败率)
