Make Android notification delivery survive idle
Schedule reconnects with idle-tolerant alarms, keep a watchdog and wake lock, and restart after task removal so delivery continues while the app is closed. Request battery exemption, stop retrying on expired sessions, persist connection diagnostics, and expose the status in Settings.
This commit is contained in:
@@ -16,6 +16,7 @@ class FakeNotificationService implements NotificationServiceBridge {
|
||||
);
|
||||
|
||||
NotificationServiceStatus current;
|
||||
List<NotificationConnectionEvent> events = const [];
|
||||
final calls = <(String, Map<String, Object?>)>[];
|
||||
final _changes = StreamController<NotificationServiceStatus>.broadcast();
|
||||
bool batteryGranted = true;
|
||||
@@ -85,4 +86,9 @@ class FakeNotificationService implements NotificationServiceBridge {
|
||||
if (batteryGranted) current = copy(batteryExempt: true);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Not recorded in [calls]: the log is re-read after every action, and the
|
||||
// tests assert on the action calls themselves.
|
||||
@override
|
||||
Future<List<NotificationConnectionEvent>> connectionEvents() async => events;
|
||||
}
|
||||
|
||||
@@ -92,5 +92,39 @@ void main() {
|
||||
final bridge = createNotificationServiceBridge();
|
||||
expect(bridge.supported, isFalse);
|
||||
expect((await bridge.status()).state, DeliveryState.off);
|
||||
expect(await bridge.connectionEvents(), isEmpty);
|
||||
});
|
||||
|
||||
test('a refused session reports sign_in_required', () {
|
||||
expect(
|
||||
NotificationServiceStatus.fromMap({'status': 'sign_in_required'}).state,
|
||||
DeliveryState.signInRequired,
|
||||
);
|
||||
});
|
||||
|
||||
test('connectionEvents reads the service log, newest first', () async {
|
||||
messenger.setMockMethodCallHandler(channel, (call) async {
|
||||
if (call.method == 'connectionEvents') {
|
||||
return [
|
||||
{
|
||||
'time': 2000,
|
||||
'event': 'failed',
|
||||
'error': 'Expected HTTP 101',
|
||||
'code': 403,
|
||||
},
|
||||
{'time': 1000, 'event': 'connected'},
|
||||
];
|
||||
}
|
||||
return reply();
|
||||
});
|
||||
final bridge = MethodChannelNotificationServiceBridge(channel);
|
||||
final events = await bridge.connectionEvents();
|
||||
expect(events.map((event) => event.event), ['failed', 'connected']);
|
||||
expect(events.first.code, 403);
|
||||
expect(events.first.error, 'Expected HTTP 101');
|
||||
expect(events.first.detail, 'HTTP 403 · Expected HTTP 101');
|
||||
expect(events.last.code, isNull);
|
||||
expect(events.last.error, isNull);
|
||||
expect(events.last.time, DateTime.fromMillisecondsSinceEpoch(1000));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -18,6 +18,9 @@ Widget _host(
|
||||
return MaterialApp(home: Scaffold(body: body));
|
||||
}
|
||||
|
||||
int _requests(FakeNotificationService service) =>
|
||||
service.calls.where((call) => call.$1 == 'requestBatteryExemption').length;
|
||||
|
||||
bool _switchValue(WidgetTester tester) {
|
||||
final tile = find.byKey(DeviceDeliveryKeys.toggle);
|
||||
return tester.widget<SwitchListTile>(tile).value;
|
||||
@@ -62,9 +65,11 @@ void main() {
|
||||
testWidgets('allow background delivery asks for the exemption', (
|
||||
tester,
|
||||
) async {
|
||||
final service = FakeNotificationService();
|
||||
final service = FakeNotificationService()..batteryGranted = false;
|
||||
await tester.pumpWidget(_host(service));
|
||||
await tester.pumpAndSettle();
|
||||
// The open-time prompt was declined; the row is the user's second try.
|
||||
service.batteryGranted = true;
|
||||
await tester.tap(find.text(deviceDeliveryBatteryLabel));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
@@ -125,6 +130,157 @@ void main() {
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(DeviceDeliveryKeys.error), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'a missing battery exemption keeps a warning row with the Samsung note',
|
||||
(tester) async {
|
||||
// The open-time prompt is declined, so the exemption stays missing.
|
||||
final service = FakeNotificationService()..batteryGranted = false;
|
||||
await tester.pumpWidget(_host(service));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(DeviceDeliveryKeys.batteryWarning), findsOneWidget);
|
||||
expect(find.text(deviceDeliveryWarningTitle), findsOneWidget);
|
||||
expect(find.textContaining(deviceDeliverySamsungNote), findsOneWidget);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('the warning goes away once the exemption is granted', (
|
||||
tester,
|
||||
) async {
|
||||
final service = FakeNotificationService()..batteryGranted = false;
|
||||
await tester.pumpWidget(_host(service));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(DeviceDeliveryKeys.batteryWarning), findsOneWidget);
|
||||
service.batteryGranted = true;
|
||||
await tester.tap(find.text(deviceDeliveryBatteryLabel));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(DeviceDeliveryKeys.batteryWarning), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('switching on asks for the battery exemption before starting', (
|
||||
tester,
|
||||
) async {
|
||||
final service = FakeNotificationService();
|
||||
service.current = service.copy(enabled: false, state: DeliveryState.off);
|
||||
await tester.pumpWidget(_host(service));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(DeviceDeliveryKeys.toggle));
|
||||
await tester.pumpAndSettle();
|
||||
final names = service.calls.map((call) => call.$1).toList();
|
||||
expect(names, contains('requestBatteryExemption'));
|
||||
expect(
|
||||
names.indexOf('requestBatteryExemption'),
|
||||
lessThan(names.indexOf('setEnabled')),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'opening Settings with delivery on and no exemption asks right away',
|
||||
(tester) async {
|
||||
final service = FakeNotificationService();
|
||||
await tester.pumpWidget(_host(service));
|
||||
await tester.pumpAndSettle();
|
||||
expect(_requests(service), 1);
|
||||
expect(
|
||||
find.text('Allowed: battery optimisation will not pause delivery'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.byKey(DeviceDeliveryKeys.batteryWarning), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('a declined open-time prompt is not repeated on resume', (
|
||||
tester,
|
||||
) async {
|
||||
final service = FakeNotificationService()..batteryGranted = false;
|
||||
await tester.pumpWidget(_host(service));
|
||||
await tester.pumpAndSettle();
|
||||
expect(_requests(service), 1);
|
||||
expect(find.byKey(DeviceDeliveryKeys.batteryWarning), findsOneWidget);
|
||||
// Returning from the system screen re-reads the status only.
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
|
||||
await tester.pumpAndSettle();
|
||||
expect(_requests(service), 1);
|
||||
expect(find.byKey(DeviceDeliveryKeys.batteryWarning), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('no open-time prompt when the exemption is already granted', (
|
||||
tester,
|
||||
) async {
|
||||
final service = FakeNotificationService();
|
||||
service.current = service.copy(batteryExempt: true);
|
||||
await tester.pumpWidget(_host(service));
|
||||
await tester.pumpAndSettle();
|
||||
expect(_requests(service), 0);
|
||||
});
|
||||
|
||||
testWidgets('no open-time prompt while delivery is off', (tester) async {
|
||||
final service = FakeNotificationService();
|
||||
service.current = service.copy(enabled: false, state: DeliveryState.off);
|
||||
await tester.pumpWidget(_host(service));
|
||||
await tester.pumpAndSettle();
|
||||
expect(_requests(service), 0);
|
||||
});
|
||||
|
||||
testWidgets('no warning while delivery is off', (tester) async {
|
||||
final service = FakeNotificationService();
|
||||
service.current = service.copy(enabled: false, state: DeliveryState.off);
|
||||
await tester.pumpWidget(_host(service));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(DeviceDeliveryKeys.batteryWarning), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('a refused session says the user must sign in again', (
|
||||
tester,
|
||||
) async {
|
||||
final service = FakeNotificationService();
|
||||
service.current = service.copy(state: DeliveryState.signInRequired);
|
||||
await tester.pumpWidget(_host(service));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
find.text('Sign in to Pipelines again to keep notifications'),
|
||||
findsOneWidget,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'the connection log lists the service events, failures included',
|
||||
(tester) async {
|
||||
final service = FakeNotificationService();
|
||||
service.events = [
|
||||
NotificationConnectionEvent(
|
||||
time: DateTime(2026, 11, 12, 9, 30, 5),
|
||||
event: 'failed',
|
||||
error: 'Expected HTTP 101',
|
||||
code: 403,
|
||||
),
|
||||
NotificationConnectionEvent(
|
||||
time: DateTime(2026, 11, 12, 9, 29),
|
||||
event: 'connected',
|
||||
),
|
||||
];
|
||||
await tester.pumpWidget(_host(service));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text(deviceDeliveryLogTitle));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Socket failed'), findsOneWidget);
|
||||
expect(
|
||||
find.textContaining('HTTP 403 · Expected HTTP 101'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(find.text('Socket opened'), findsOneWidget);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('an empty connection log says so', (tester) async {
|
||||
final service = FakeNotificationService();
|
||||
await tester.pumpWidget(_host(service));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text(deviceDeliveryLogTitle));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text(deviceDeliveryLogEmpty), findsOneWidget);
|
||||
});
|
||||
}
|
||||
|
||||
class _FailingService extends FakeNotificationService {
|
||||
|
||||
Reference in New Issue
Block a user