|
| 1 | +# NotificationManager |
| 2 | + |
| 3 | +`NotificationManager` is a system-wide framework for posting, displaying, and dispatching notifications. It is inspired by Android's notification system, but tailored for resource-constrained devices. |
| 4 | + |
| 5 | +Notifications appear in the top notification bar and in the pull-down drawer. Tapping a notification dispatches its attached `Intent`, which can open an activity or start an app. |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +- Apps post `Notification` objects through `NotificationManager.notify()`. |
| 10 | +- The system UI listens for changes and updates the notification bar and drawer automatically. |
| 11 | +- Notifications are persisted to storage (with a debounced write) so they survive reboots. |
| 12 | +- Notifications can be cancelled individually, in bulk, or automatically when tapped. |
| 13 | + |
| 14 | +## Basic Usage |
| 15 | + |
| 16 | +```python |
| 17 | +from mpos import NotificationManager, Notification, Intent |
| 18 | + |
| 19 | +notification = Notification( |
| 20 | + notification_id="myapp.event", |
| 21 | + icon=lv.SYMBOL.BELL, |
| 22 | + title="Event", |
| 23 | + text="Something happened!", |
| 24 | + intent=Intent(action="main", app_fullname="com.example.myapp"), |
| 25 | + auto_cancel=True, |
| 26 | +) |
| 27 | + |
| 28 | +NotificationManager.notify(notification) |
| 29 | +``` |
| 30 | + |
| 31 | +To remove a notification: |
| 32 | + |
| 33 | +```python |
| 34 | +NotificationManager.cancel("myapp.event") |
| 35 | +``` |
| 36 | + |
| 37 | +## Notification Object |
| 38 | + |
| 39 | +`Notification` is a plain data object that describes a single notification. |
| 40 | + |
| 41 | +### Constructor Arguments |
| 42 | + |
| 43 | +| Argument | Type | Description | |
| 44 | +|----------|------|-------------| |
| 45 | +| `notification_id` | `str` | Unique identifier. Also accepts `uniqueidString` for compatibility. Required. | |
| 46 | +| `icon` | `str` or `lv.image_dsc_t` | Icon shown in the bar/drawer. Can be an `lv.SYMBOL.*` string, another string, or an LVGL image descriptor. | |
| 47 | +| `title` | `str` | Short title. | |
| 48 | +| `text` | `str` | Longer body text. | |
| 49 | +| `priority` | `int` | One of `Notification.PRIORITY_MIN`, `PRIORITY_LOW`, `PRIORITY_DEFAULT`, `PRIORITY_HIGH`, `PRIORITY_MAX`. Higher priority notifications are shown first. | |
| 50 | +| `intent` | `Intent` | `Intent` to dispatch when the user taps the notification. | |
| 51 | +| `auto_cancel` | `bool` | If `True` (default), the notification is cancelled automatically after it is tapped. | |
| 52 | +| `app_fullname` | `str` | App that owns the notification. Used as a fallback target if the intent has no explicit target. | |
| 53 | + |
| 54 | +### Priority Levels |
| 55 | + |
| 56 | +```python |
| 57 | +Notification.PRIORITY_MIN = -1 |
| 58 | +Notification.PRIORITY_LOW = 0 |
| 59 | +Notification.PRIORITY_DEFAULT = 1 |
| 60 | +Notification.PRIORITY_HIGH = 2 |
| 61 | +Notification.PRIORITY_MAX = 3 |
| 62 | +``` |
| 63 | + |
| 64 | +The drawer sorts notifications by priority, then by most recent update time. |
| 65 | + |
| 66 | +## NotificationManager API |
| 67 | + |
| 68 | +All methods are class methods. `NotificationManager` initializes itself lazily on first use. |
| 69 | + |
| 70 | +### `notify(notification)` |
| 71 | + |
| 72 | +Post or update a notification. |
| 73 | + |
| 74 | +- If a notification with the same ID already exists, its content is updated in place without a new persistence flash. |
| 75 | +- If it is new, it is added, the list is trimmed to `MAX_NOTIFICATIONS` (20), and persistence is scheduled. |
| 76 | + |
| 77 | +```python |
| 78 | +NotificationManager.notify(Notification( |
| 79 | + notification_id="osupdate.available", |
| 80 | + icon=lv.SYMBOL.DOWNLOAD, |
| 81 | + title="Update available", |
| 82 | + text="MicroPythonOS 1.2.3 is ready to install", |
| 83 | + priority=Notification.PRIORITY_HIGH, |
| 84 | +)) |
| 85 | +``` |
| 86 | + |
| 87 | +### `cancel(notification_id)` |
| 88 | + |
| 89 | +Remove a single notification. Writes immediately so the notification does not reappear after reboot. |
| 90 | + |
| 91 | +```python |
| 92 | +NotificationManager.cancel("osupdate.available") |
| 93 | +``` |
| 94 | + |
| 95 | +### `cancel_all()` |
| 96 | + |
| 97 | +Remove all notifications. |
| 98 | + |
| 99 | +```python |
| 100 | +NotificationManager.cancel_all() |
| 101 | +``` |
| 102 | + |
| 103 | +### `get_notifications()` |
| 104 | + |
| 105 | +Return a sorted list of active notifications, highest priority first. |
| 106 | + |
| 107 | +```python |
| 108 | +for n in NotificationManager.get_notifications(): |
| 109 | + print(n.title, n.text) |
| 110 | +``` |
| 111 | + |
| 112 | +### `get_notification(notification_id)` |
| 113 | + |
| 114 | +Return a single notification by ID, or `None` if it doesn't exist. |
| 115 | + |
| 116 | +```python |
| 117 | +n = NotificationManager.get_notification("osupdate.available") |
| 118 | +``` |
| 119 | + |
| 120 | +### `trigger(notification_id_or_object)` |
| 121 | + |
| 122 | +Dispatch the notification's intent. This is what the system calls when the user taps a notification in the drawer. |
| 123 | + |
| 124 | +- If the intent has an explicit `activity_class` or `action`, `ActivityNavigator.startActivity()` is used. |
| 125 | +- Otherwise, the owning `app_fullname` is started via `AppManager.start_app()`. |
| 126 | +- If `auto_cancel` is enabled and the dispatch succeeds, the notification is cancelled. |
| 127 | + |
| 128 | +```python |
| 129 | +NotificationManager.trigger("osupdate.available") |
| 130 | +``` |
| 131 | + |
| 132 | +### `register_listener(callback, notify_immediately=True)` / `unregister_listener(callback)` |
| 133 | + |
| 134 | +Register a function to be called whenever notifications change. The top menu/drawer uses this to refresh the UI. |
| 135 | + |
| 136 | +```python |
| 137 | +def on_notifications_changed(): |
| 138 | + print("Notifications updated") |
| 139 | + |
| 140 | +NotificationManager.register_listener(on_notifications_changed) |
| 141 | +``` |
| 142 | + |
| 143 | +## Complete Example |
| 144 | + |
| 145 | +```python |
| 146 | +import lvgl as lv |
| 147 | +from mpos import Activity, NotificationManager, Notification, Intent |
| 148 | + |
| 149 | +class MyApp(Activity): |
| 150 | + |
| 151 | + def show_notification(self): |
| 152 | + intent = Intent(action="main", app_fullname="com.example.myapp") |
| 153 | + notification = Notification( |
| 154 | + notification_id="com.example.myapp.done", |
| 155 | + icon=lv.SYMBOL.OK, |
| 156 | + title="Done", |
| 157 | + text="The operation finished successfully.", |
| 158 | + intent=intent, |
| 159 | + auto_cancel=True, |
| 160 | + app_fullname="com.example.myapp", |
| 161 | + ) |
| 162 | + NotificationManager.notify(notification) |
| 163 | +``` |
| 164 | + |
| 165 | +## Best Practices |
| 166 | + |
| 167 | +### Do's |
| 168 | + |
| 169 | +✅ Use a stable, unique `notification_id` so the same event doesn't create duplicate notifications |
| 170 | +✅ Set a meaningful `app_fullname` so tapping the notification can fall back to launching your app |
| 171 | +✅ Cancel notifications when they are no longer relevant |
| 172 | +✅ Use `auto_cancel=True` for one-shot notifications that should disappear after being tapped |
| 173 | + |
| 174 | +### Don'ts |
| 175 | + |
| 176 | +❌ Don't rely on `lv.image_dsc_t` icons surviving a reboot — only string icons are persisted |
| 177 | +❌ Don't post large amounts of text; storage and display space are limited |
| 178 | +❌ Don't use more than `MAX_NOTIFICATIONS` (20) active notifications |
| 179 | + |
| 180 | +## See Also |
| 181 | + |
| 182 | +- [Service](service.md) — Background services that often post notifications at boot |
| 183 | +- [SettingActivity](setting-activity.md) — Per-app settings storage, useful with notification preferences |
| 184 | +- [App Lifecycle](../apps/app-lifecycle.md) — Foreground handling when notifications launch your app |
0 commit comments