Fix review findings in low battery alerts

- Respect the notifications-enabled setting
- Notify only the lowest newly crossed threshold
- Ignore implausible readings (boards without a battery sensor report 0 mV)
- Wait for the device identity so the saved chemistry is used
- Require two consecutive low readings before alerting
- Re-arm and clear alerts on any link reset, not just explicit disconnect
- Cancel stale notifications on re-arm and link reset
- Scope notification ids per device and name the device in the body
- Use the connector's shared NotificationService
This commit is contained in:
zjs81 committed 2026-10-24 20:45:14 -07:00
1 parent 7d55bf03df
commit b6c1d73bf0
22 files changed
+138 -52

No files matched your search

+28 -5
View File
@@ -600,10 +600,13 @@ class NotificationService {
/// Posts a low-battery alert for the connected device.
///
/// Uses a per-threshold notification id so each alert level updates its own
/// notification instead of stacking. Bypasses the rate-limit queue, which
/// only handles message and advert notifications.
/// Uses a per-device, per-threshold notification id so each alert level
/// updates its own notification instead of stacking, and alerts for
/// different radios don't overwrite each other. Bypasses the rate-limit
/// queue, which only handles message and advert notifications.
Future<void> showLowBatteryNotification({
required String deviceKey,
required String deviceName,
required int batteryPercent,
required int threshold,
}) async {
@@ -632,9 +635,9 @@ class NotificationService {
try {
await _notifications.show(
id: 'low_battery:$threshold'.hashCode,
id: _lowBatteryNotificationId(deviceKey, threshold),
title: _l10n.notification_lowBatteryTitle,
body: _l10n.notification_lowBatteryBody(batteryPercent),
body: _l10n.notification_lowBatteryBody(deviceName, batteryPercent),
notificationDetails: notificationDetails,
payload: 'low_battery',
);
@@ -699,6 +702,26 @@ class NotificationService {
await _updateBadge(totalUnreadCount);
}
static int _lowBatteryNotificationId(String deviceKey, int threshold) =>
'low_battery:$deviceKey:$threshold'.hashCode;
/// Cancel a device's low-battery notifications for the given thresholds.
Future<void> clearLowBatteryNotifications(
String deviceKey,
Iterable<int> thresholds,
) async {
if (!await _ensureInitialized()) return;
for (final threshold in thresholds) {
try {
await _notifications.cancel(
id: _lowBatteryNotificationId(deviceKey, threshold),
);
} catch (e) {
debugPrint('Failed to clear low battery notification: $e');
}
}
}
/// Cancel advert notifications for the given contact public key hexes.
Future<void> clearAdvertNotifications(List<String> contactIds) async {
if (!await _ensureInitialized()) return;