Add low battery notification channel with threshold alerts

This commit is contained in:
shortwavesurfer2009 committed 2026-10-04 01:26:36 -04:00
1 parent 9334d556fe
commit 296eab20ca
23 files changed
+327 -1

No files matched your search

+27
View File
@@ -266,6 +266,10 @@ class MeshCoreConnector extends ChangeNotifier {
Stopwatch? _airtimeBumpStopwatch;
int _prevTotalAirSecs = 0;
int? _batteryMillivolts;
// Thresholds (percent) that trigger a low-battery notification once per
// crossing, re-arming 5 points above the threshold after recharge.
static const List<int> _lowBatteryThresholds = [25, 10];
final Set<int> _batteryAlertedThresholds = {};
double? _selfLatitude;
double? _selfLongitude;
final List<DirectRepeater> _directRepeaters = List.empty(growable: true);
@@ -3139,6 +3143,7 @@ class MeshCoreConnector extends ChangeNotifier {
_autoAddMaxHops = null;
_contactsStorageFull = false;
_batteryMillivolts = null;
_batteryAlertedThresholds.clear();
_repeaterBatterySnapshots.clear();
_batteryRequested = false;
_awaitingSelfInfo = false;
@@ -5357,6 +5362,7 @@ class MeshCoreConnector extends ChangeNotifier {
_batteryMillivolts = reader.readUInt16LE();
_storageUsedKb = reader.readUInt32LE();
_storageTotalKb = reader.readUInt32LE();
_checkLowBatteryThresholds();
final volts = (_batteryMillivolts! / 1000.0).toStringAsFixed(2);
_appDebugLogService?.info(
'Pulled battery: $volts V ($_batteryMillivolts mV)',
@@ -5371,6 +5377,27 @@ class MeshCoreConnector extends ChangeNotifier {
}
}
void _checkLowBatteryThresholds() {
final percent = batteryPercent;
if (percent == null) return;
for (final threshold in _lowBatteryThresholds) {
if (percent <= threshold &&
!_batteryAlertedThresholds.contains(threshold)) {
_batteryAlertedThresholds.add(threshold);
NotificationService().showLowBatteryNotification(
batteryPercent: percent,
threshold: threshold,
);
} else if (percent > threshold + 5 &&
_batteryAlertedThresholds.contains(threshold)) {
// Re-arm once the battery recovers 5 points above the threshold,
// so a recharge + discharge cycle alerts again without flapping.
_batteryAlertedThresholds.remove(threshold);
}
}
}
void _checkManualAddContacts() async {
// If manual add contacts is enabled, set auto add config and other params.
// and disable it after
+13
View File
@@ -2573,6 +2573,19 @@
"contacts_contactAdvertCopied": "Advert copied to Clipboard.",
"contacts_contactAdvertCopyFailed": "Copying advert to Clipboard failed.",
"notification_activityTitle": "MeshCore Activity",
"notification_lowBatteryTitle": "Low battery",
"@notification_lowBatteryTitle": {
"description": "Title of the low battery notification"
},
"notification_lowBatteryBody": "Device battery is at {percent}%",
"@notification_lowBatteryBody": {
"description": "Body of the low battery notification",
"placeholders": {
"percent": {
"type": "int"
}
}
},
"notification_messagesCount": "{count} {count, plural, =1{message} other{messages}}",
"@notification_messagesCount": {
"placeholders": {
+12
View File
@@ -7924,6 +7924,18 @@ abstract class AppLocalizations {
/// **'MeshCore Activity'**
String get notification_activityTitle;
/// Title of the low battery notification
///
/// In en, this message translates to:
/// **'Low battery'**
String get notification_lowBatteryTitle;
/// Body of the low battery notification
///
/// In en, this message translates to:
/// **'Device battery is at {percent}%'**
String notification_lowBatteryBody(int percent);
/// No description provided for @notification_messagesCount.
///
/// In en, this message translates to:
+8
View File
@@ -4597,6 +4597,14 @@ class AppLocalizationsBg extends AppLocalizations {
@override
String get notification_activityTitle => 'Активност на MeshCore';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4608,6 +4608,14 @@ class AppLocalizationsDe extends AppLocalizations {
@override
String get notification_activityTitle => 'MeshCore Aktivität';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4533,6 +4533,14 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get notification_activityTitle => 'MeshCore Activity';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4599,6 +4599,14 @@ class AppLocalizationsEs extends AppLocalizations {
@override
String get notification_activityTitle => 'Actividad de MeshCore';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4619,6 +4619,14 @@ class AppLocalizationsFr extends AppLocalizations {
@override
String get notification_activityTitle => 'Activité MeshCore';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4597,6 +4597,14 @@ class AppLocalizationsHu extends AppLocalizations {
@override
String get notification_activityTitle => 'MeshCore tevékenység';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4604,6 +4604,14 @@ class AppLocalizationsIt extends AppLocalizations {
@override
String get notification_activityTitle => 'Attività MeshCore';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4373,6 +4373,14 @@ class AppLocalizationsJa extends AppLocalizations {
@override
String get notification_activityTitle => 'メッシュコアの活動';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4384,6 +4384,14 @@ class AppLocalizationsKo extends AppLocalizations {
@override
String get notification_activityTitle => '메쉬코어 활동';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4575,6 +4575,14 @@ class AppLocalizationsNl extends AppLocalizations {
@override
String get notification_activityTitle => 'MeshCore Activiteit';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4623,6 +4623,14 @@ class AppLocalizationsPl extends AppLocalizations {
@override
String get notification_activityTitle => 'Aktywność MeshCore';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4601,6 +4601,14 @@ class AppLocalizationsPt extends AppLocalizations {
@override
String get notification_activityTitle => 'Atividade MeshCore';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4607,6 +4607,14 @@ class AppLocalizationsRu extends AppLocalizations {
@override
String get notification_activityTitle => 'Активность MeshCore';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4581,6 +4581,14 @@ class AppLocalizationsSk extends AppLocalizations {
@override
String get notification_activityTitle => 'Aktivita MeshCore';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4579,6 +4579,14 @@ class AppLocalizationsSl extends AppLocalizations {
@override
String get notification_activityTitle => 'Aktivnost MeshCore';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4557,6 +4557,14 @@ class AppLocalizationsSv extends AppLocalizations {
@override
String get notification_activityTitle => 'MeshCore Aktivitet';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4607,6 +4607,14 @@ class AppLocalizationsUk extends AppLocalizations {
@override
String get notification_activityTitle => 'Активність MeshCore';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
String _temp0 = intl.Intl.pluralLogic(
+8
View File
@@ -4285,6 +4285,14 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String get notification_activityTitle => 'MeshCore 活动';
@override
String get notification_lowBatteryTitle => 'Low battery';
@override
String notification_lowBatteryBody(int percent) {
return 'Device battery is at $percent%';
}
@override
String notification_messagesCount(int count) {
return '$count 条消息';
+45
View File
@@ -597,6 +597,51 @@ 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.
Future<void> showLowBatteryNotification({
required int batteryPercent,
required int threshold,
}) async {
if (!await _ensureCanNotify()) return;
const androidDetails = AndroidNotificationDetails(
'low_battery',
'Low Battery',
channelDescription: 'Low battery alerts for the connected device',
importance: Importance.high,
priority: Priority.high,
icon: '@mipmap/ic_launcher',
);
const darwinDetails = DarwinNotificationDetails(
presentAlert: true,
presentBadge: false,
presentSound: true,
);
const notificationDetails = NotificationDetails(
android: androidDetails,
iOS: darwinDetails,
macOS: darwinDetails,
);
try {
await _notifications.show(
id: 'low_battery:$threshold'.hashCode,
title: _l10n.notification_lowBatteryTitle,
body: _l10n.notification_lowBatteryBody(batteryPercent),
notificationDetails: notificationDetails,
payload: 'low_battery',
);
} catch (e) {
debugPrint('Failed to show low battery notification: $e');
}
}
/// Returns a privacy-safe identifier for debug logging.
/// - advert: shows device name (body contains contactName)
/// - message: shows "from: sender" (avoids logging message content)
+86 -1
View File
@@ -1 +1,86 @@
{}
{
"bg": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"de": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"es": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"fr": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"hu": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"it": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"ja": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"ko": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"nl": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"pl": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"pt": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"ru": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"sk": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"sl": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"sv": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"uk": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
],
"zh": [
"notification_lowBatteryTitle",
"notification_lowBatteryBody"
]
}