Deliver server notifications from the Android app itself
A native foreground service keeps an authenticated websocket to the connected server (handed over by the app on every connect), subscribes with a replay cursor, shows each notification with kind-based channels and grouping, opens pipelines:// links on tap, and clears taps, dismissals and cross-device clears. It starts at boot and app launch, backs off and follows network changes. Settings gains the delivery switch, status, notification and battery-exemption actions, and the server's optional ntfy_enabled toggle.
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/notification_service/notification_service_bridge.dart';
|
||||
import 'package:pipelines_app/features/settings/settings_device_delivery_section.dart';
|
||||
|
||||
import '../notification_service/fake_notification_service.dart';
|
||||
|
||||
Widget _host(
|
||||
NotificationServiceBridge bridge, {
|
||||
required bool cupertino,
|
||||
Future<void> Function()? requestPermission,
|
||||
}) => MaterialApp(
|
||||
theme: ThemeData(
|
||||
platform: cupertino ? TargetPlatform.iOS : TargetPlatform.android,
|
||||
),
|
||||
home: Scaffold(
|
||||
body: SingleChildScrollView(
|
||||
child: SettingsDeviceDeliverySection(
|
||||
bridge: bridge,
|
||||
requestNotificationPermission: requestPermission,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
bool _switchValue(WidgetTester tester, {required bool cupertino}) {
|
||||
final tile = find.byKey(DeviceDeliveryKeys.toggle);
|
||||
if (cupertino) {
|
||||
return tester
|
||||
.widget<CupertinoSwitch>(
|
||||
find.descendant(of: tile, matching: find.byType(CupertinoSwitch)),
|
||||
)
|
||||
.value;
|
||||
}
|
||||
return tester.widget<SwitchListTile>(tile).value;
|
||||
}
|
||||
|
||||
void main() {
|
||||
for (final cupertino in [false, true]) {
|
||||
final style = cupertino ? 'Cupertino' : 'Material';
|
||||
|
||||
testWidgets('$style shows the switch, status and ntfy note', (
|
||||
tester,
|
||||
) async {
|
||||
final service = FakeNotificationService();
|
||||
await tester.pumpWidget(_host(service, cupertino: cupertino));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text(deviceDeliverySwitchLabel), findsOneWidget);
|
||||
expect(_switchValue(tester, cupertino: cupertino), isTrue);
|
||||
expect(find.text('Connected'), findsOneWidget);
|
||||
expect(find.text(deviceDeliveryNote), findsOneWidget);
|
||||
expect(find.text(deviceDeliveryBatteryLabel), findsOneWidget);
|
||||
expect(find.byKey(DeviceDeliveryKeys.permission), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('$style switching off stops delivery', (tester) async {
|
||||
final service = FakeNotificationService();
|
||||
await tester.pumpWidget(_host(service, cupertino: cupertino));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(DeviceDeliveryKeys.toggle));
|
||||
await tester.pumpAndSettle();
|
||||
expect(service.calls.last.$1, 'setEnabled');
|
||||
expect(service.calls.last.$2, {'enabled': false});
|
||||
expect(_switchValue(tester, cupertino: cupertino), isFalse);
|
||||
expect(find.text('Off'), findsOneWidget);
|
||||
// The grants only matter while delivery is on.
|
||||
expect(find.text(deviceDeliveryBatteryLabel), findsNothing);
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('live status pushed by the service is shown', (tester) async {
|
||||
final service = FakeNotificationService();
|
||||
await tester.pumpWidget(_host(service, cupertino: false));
|
||||
await tester.pumpAndSettle();
|
||||
service.push(service.copy(state: DeliveryState.reconnecting));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Reconnecting…'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('allow background delivery asks for the exemption', (
|
||||
tester,
|
||||
) async {
|
||||
final service = FakeNotificationService();
|
||||
await tester.pumpWidget(_host(service, cupertino: false));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text(deviceDeliveryBatteryLabel));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
service.calls.map((call) => call.$1),
|
||||
contains('requestBatteryExemption'),
|
||||
);
|
||||
expect(
|
||||
find.text('Allowed: battery optimisation will not pause delivery'),
|
||||
findsOneWidget,
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('blocked notifications offer the permission request', (
|
||||
tester,
|
||||
) async {
|
||||
var asked = 0;
|
||||
final service = FakeNotificationService();
|
||||
service.current = service.copy(notificationsAllowed: false);
|
||||
await tester.pumpWidget(
|
||||
_host(
|
||||
service,
|
||||
cupertino: false,
|
||||
requestPermission: () async {
|
||||
asked++;
|
||||
service.current = service.copy(notificationsAllowed: true);
|
||||
},
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(DeviceDeliveryKeys.permission));
|
||||
await tester.pumpAndSettle();
|
||||
expect(asked, 1);
|
||||
expect(find.byKey(DeviceDeliveryKeys.permission), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('switching on asks for notifications when blocked', (
|
||||
tester,
|
||||
) async {
|
||||
var asked = 0;
|
||||
final service = FakeNotificationService();
|
||||
service.current = service.copy(
|
||||
enabled: false,
|
||||
state: DeliveryState.off,
|
||||
notificationsAllowed: false,
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
_host(service, cupertino: false, requestPermission: () async => asked++),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(DeviceDeliveryKeys.toggle));
|
||||
await tester.pumpAndSettle();
|
||||
expect(asked, 1);
|
||||
expect(service.calls.last.$1, 'setEnabled');
|
||||
expect(service.calls.last.$2, {'enabled': true});
|
||||
});
|
||||
|
||||
testWidgets('a failing platform is reported, not hidden', (tester) async {
|
||||
await tester.pumpWidget(_host(_FailingService(), cupertino: false));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(DeviceDeliveryKeys.error), findsOneWidget);
|
||||
});
|
||||
}
|
||||
|
||||
class _FailingService extends FakeNotificationService {
|
||||
@override
|
||||
Future<NotificationServiceStatus> status() async =>
|
||||
throw StateError('no service');
|
||||
}
|
||||
@@ -200,4 +200,50 @@ void main() {
|
||||
isFalse,
|
||||
);
|
||||
});
|
||||
|
||||
group('ntfy_enabled', () {
|
||||
JsonMap withNtfy(bool ntfy) => {
|
||||
'type': 'notification_settings',
|
||||
'notification_settings': {
|
||||
...(_reply(true, true, false)['notification_settings'] as Map),
|
||||
'ntfy_enabled': ntfy,
|
||||
},
|
||||
};
|
||||
|
||||
testWidgets('hidden when the server does not send it', (tester) async {
|
||||
final gateway = _Gateway((_, _) => _reply(true, true, false));
|
||||
await tester.pumpWidget(_host(gateway, cupertino: false));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(ntfySettingKey('ntfy_enabled')), findsNothing);
|
||||
expect(find.text(ntfySectionSubtitle), findsOneWidget);
|
||||
});
|
||||
|
||||
for (final cupertino in [false, true]) {
|
||||
testWidgets(
|
||||
'${cupertino ? 'Cupertino' : 'Material'} shows and writes it',
|
||||
(tester) async {
|
||||
final gateway = _Gateway(
|
||||
(type, _) => withNtfy(type == 'set_notification_settings'),
|
||||
);
|
||||
await tester.pumpWidget(_host(gateway, cupertino: cupertino));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Also send to ntfy'), findsOneWidget);
|
||||
expect(find.text(ntfySectionAppSubtitle), findsOneWidget);
|
||||
expect(
|
||||
_checked(tester, 'ntfy_enabled', cupertino: cupertino),
|
||||
isFalse,
|
||||
);
|
||||
await tester.tap(find.byKey(ntfySettingKey('ntfy_enabled')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.calls.last.$2, {
|
||||
'notification_settings': {'ntfy_enabled': true},
|
||||
});
|
||||
expect(
|
||||
_checked(tester, 'ntfy_enabled', cupertino: cupertino),
|
||||
isTrue,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user