Skip to content

Commit fad4ff6

Browse files
authored
fix(mobile): hide notification settings without relay push capability (#8025)
Hide the iOS Notifications settings section for communities that have not enabled push unless their relay advertises valid push support. A build with a configured gateway previously showed the switch even when the relay could not support push. Communities where push is already enabled retain an off switch and iOS Settings recovery during discovery or when push support is unavailable. The section explains that support is unavailable instead of claiming notifications will arrive. Capability discovery keeps its existing refresh behavior. Returning after at least five seconds in the background reconnects the session and fetches capability again. Tom explicitly chose this behavior without new retries, polling, or refresh triggers. Companion client change for squareup/buzz-releases#104. Related: #7826 (broader notification availability diagnostics). ### Validation Settings widget tests cover hidden first-time opt-in during loading, absent capability, and discovery errors. Recovery tests exercise the real off switch for already-enabled communities in all three states, verifying persisted opt-out, the native snapshot writer, a durable pending relay tombstone after failure, and the iOS Settings link. They also verify that pending recovery stays visible after a relay failure and disappears only after opt-out confirmation. Tests without a gateway definition verify hidden first-time settings and disabled native registration. Mobile analysis and the mobile test suite passed. --------- Signed-off-by: Tom Brow <[email protected]>
1 parent 2cca0ec commit fad4ff6

5 files changed

Lines changed: 335 additions & 64 deletions

File tree

‎mobile/lib/features/settings/settings_page.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import '../../shared/auth/auth.dart';
1313
import '../../shared/clipboard_utils.dart';
1414
import '../../shared/community/community_membership_provider.dart';
1515
import '../../shared/push/push_bridge.dart';
16+
import '../../shared/push/push_relay_capability_provider.dart';
1617
import '../../shared/relay/relay.dart';
1718
import '../../shared/utils/string_utils.dart';
1819
import '../pairing/pairing_provider.dart';

‎mobile/lib/features/settings/settings_page/notifications_section.dart‎

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,33 @@ class _NotificationsSection extends ConsumerWidget {
1010
}
1111
final community = ref.watch(activeCommunityProvider).value;
1212
if (community == null) return const SizedBox.shrink();
13-
if (!Env.pushGatewayConfigured) {
14-
return AppListCard(
15-
label: 'Notifications',
16-
verticalPadding: Grid.twelve,
17-
children: [
18-
AppListRow(
19-
key: const ValueKey('push-notifications-unavailable'),
20-
icon: LucideIcons.bell,
21-
title: 'Push notifications',
22-
subtitle: 'Unavailable in this build',
23-
),
24-
],
25-
);
13+
final capability = ref.watch(currentRelayPushDescriptorProvider);
14+
final hasCapability =
15+
!capability.isLoading &&
16+
!capability.hasError &&
17+
capability.value != null;
18+
final optOutPending =
19+
community.pushSubscriptionState.pendingTombstoneGeneration != null;
20+
if (!hasCapability &&
21+
!community.pushNotificationsEnabled &&
22+
!optOutPending) {
23+
return const SizedBox.shrink();
2624
}
2725
final authorization = ref.watch(buzzPushAuthorizationStatusProvider);
2826
final status = authorization.value;
2927
final permissionUnavailable = authorization.hasError;
3028
final permissionDenied = status == BuzzPushAuthorizationStatus.denied;
3129
final showSettingsRecovery =
32-
community.pushNotificationsEnabled &&
33-
(permissionDenied || permissionUnavailable);
34-
final subtitle = !community.pushNotificationsEnabled
30+
optOutPending ||
31+
(community.pushNotificationsEnabled &&
32+
(permissionDenied || permissionUnavailable));
33+
final canToggle = hasCapability || community.pushNotificationsEnabled;
34+
final subtitle = optOutPending
35+
? 'Waiting for relay confirmation; notifications may continue'
36+
: !community.pushNotificationsEnabled
3537
? 'Off for this community'
38+
: !hasCapability
39+
? 'Push support unavailable; you can still turn notifications off'
3640
: switch (status) {
3741
BuzzPushAuthorizationStatus.notDetermined =>
3842
'Waiting for iOS notification permission',
@@ -63,20 +67,24 @@ class _NotificationsSection extends ConsumerWidget {
6367
: null,
6468
trailing: Switch.adaptive(
6569
value: community.pushNotificationsEnabled,
66-
onChanged: (enabled) => unawaited(
67-
ref
68-
.read(communityListProvider.notifier)
69-
.setPushNotificationsEnabled(community.id, enabled),
70-
),
70+
onChanged: !canToggle
71+
? null
72+
: (enabled) => unawaited(
73+
ref
74+
.read(communityListProvider.notifier)
75+
.setPushNotificationsEnabled(community.id, enabled),
76+
),
7177
),
72-
onTap: () => unawaited(
73-
ref
74-
.read(communityListProvider.notifier)
75-
.setPushNotificationsEnabled(
76-
community.id,
77-
!community.pushNotificationsEnabled,
78+
onTap: !canToggle
79+
? null
80+
: () => unawaited(
81+
ref
82+
.read(communityListProvider.notifier)
83+
.setPushNotificationsEnabled(
84+
community.id,
85+
!community.pushNotificationsEnabled,
86+
),
7887
),
79-
),
8088
),
8189
if (showSettingsRecovery)
8290
AppListRow(
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import 'dart:async';
2+
3+
import 'package:buzz/features/settings/settings_page.dart';
4+
import 'package:buzz/shared/auth/auth_provider.dart';
5+
import 'package:buzz/shared/community/community.dart';
6+
import 'package:buzz/shared/community/community_provider.dart';
7+
import 'package:buzz/shared/community/community_storage.dart';
8+
import 'package:buzz/shared/push/dev_push_lease.dart';
9+
import 'package:buzz/shared/push/push_bridge.dart';
10+
import 'package:buzz/shared/push/push_relay_capability_provider.dart';
11+
import 'package:buzz/shared/push/push_subscription.dart';
12+
import 'package:buzz/shared/relay/app_lifecycle_provider.dart';
13+
import 'package:buzz/shared/theme/theme.dart';
14+
import 'package:flutter/foundation.dart';
15+
import 'package:flutter/material.dart';
16+
import 'package:flutter_test/flutter_test.dart';
17+
import 'package:hooks_riverpod/hooks_riverpod.dart';
18+
import 'package:package_info_plus/package_info_plus.dart';
19+
import 'package:shared_preferences/shared_preferences.dart';
20+
21+
import '../../shared/community/community_storage_test.dart';
22+
23+
void main() {
24+
for (final outcome in ['loading', 'absent', 'error']) {
25+
testWidgets('enabled community can turn push off with $outcome capability', (
26+
tester,
27+
) async {
28+
tester.view.physicalSize = const Size(430, 1800);
29+
tester.view.devicePixelRatio = 1;
30+
addTearDown(tester.view.reset);
31+
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
32+
addTearDown(() => debugDefaultTargetPlatformOverride = null);
33+
PackageInfo.setMockInitialValues(
34+
appName: 'Buzz',
35+
packageName: 'buzz',
36+
version: '1.0.0',
37+
buildNumber: '1',
38+
buildSignature: '',
39+
);
40+
SharedPreferences.setMockInitialValues({});
41+
final prefs = await SharedPreferences.getInstance();
42+
final storage = CommunityStorage(secure: FakeSecureStorage());
43+
final subscriptions = [
44+
BuzzPushSubscription(
45+
filter: BuzzPushFilter(kinds: const [9], pTags: ['a' * 64]),
46+
notificationClass: 'default',
47+
),
48+
];
49+
final community =
50+
Community.create(
51+
name: 'Team',
52+
relayUrl: 'https://relay.example',
53+
).copyWith(
54+
pushNotificationsEnabled: true,
55+
pushSubscriptionState: BuzzPushLeaseSubscriptionState.desired(
56+
desired: subscriptions,
57+
).withAccepted(subscriptions: subscriptions, generation: 7),
58+
);
59+
await storage.save(community);
60+
await storage.saveActiveId(community.id);
61+
final pending = Completer<BuzzPushLeaseDescriptor?>();
62+
final snapshots = <List<Community>>[];
63+
final tombstones = <int?>[];
64+
var settingsOpened = 0;
65+
var relayAvailable = false;
66+
await tester.pumpWidget(
67+
ProviderScope(
68+
overrides: [
69+
savedPrefsProvider.overrideWithValue(prefs),
70+
authProvider.overrideWith(_Auth.new),
71+
communityStorageProvider.overrideWithValue(storage),
72+
communitySnapshotWriterProvider.overrideWithValue((
73+
communities,
74+
) async {
75+
snapshots.add(List.of(communities));
76+
}),
77+
communityPushLeaseDeactivatorProvider.overrideWithValue((
78+
community, {
79+
generation,
80+
}) async {
81+
tombstones.add(generation);
82+
if (!relayAvailable) throw StateError('relay unavailable');
83+
}),
84+
currentRelayPushDescriptorProvider.overrideWith((ref) {
85+
if (outcome == 'loading') return pending.future;
86+
if (outcome == 'error') throw StateError('discovery failed');
87+
return Future.value(null);
88+
}),
89+
appLifecycleProvider.overrideWith(_Lifecycle.new),
90+
buzzPushAuthorizationStatusReaderProvider.overrideWithValue(
91+
() async => BuzzPushAuthorizationStatus.denied,
92+
),
93+
buzzPushNotificationSettingsOpenerProvider.overrideWithValue(
94+
() async {
95+
settingsOpened++;
96+
return true;
97+
},
98+
),
99+
],
100+
child: MaterialApp(
101+
theme: AppTheme.light(),
102+
home: SettingsPage(
103+
profileHeader: const SizedBox.shrink(),
104+
invitePageBuilder: (_) => const SizedBox.shrink(),
105+
identityRecoveryPageBuilder: (_) => const SizedBox.shrink(),
106+
),
107+
),
108+
),
109+
);
110+
await tester.pumpAndSettle();
111+
final container = ProviderScope.containerOf(
112+
tester.element(find.byType(SettingsPage)),
113+
);
114+
expect(
115+
container.read(activeCommunityProvider).value?.pushNotificationsEnabled,
116+
isTrue,
117+
reason:
118+
'${container.read(activeCommunityProvider)} / ${container.read(communityListProvider)}',
119+
);
120+
expect(tester.widget<Switch>(find.byType(Switch)).value, isTrue);
121+
expect(
122+
find.text(
123+
'Push support unavailable; you can still turn notifications off',
124+
),
125+
findsOneWidget,
126+
);
127+
await tester.tap(
128+
find.byKey(const ValueKey('push-notifications-open-settings')),
129+
);
130+
await tester.pump();
131+
expect(settingsOpened, 1);
132+
await tester.tap(find.byType(Switch));
133+
await tester.pumpAndSettle();
134+
final stored = (await storage.loadAll()).single;
135+
expect(stored.pushNotificationsEnabled, isFalse);
136+
expect(stored.pushSubscriptionState.pendingTombstoneGeneration, 8);
137+
expect(tombstones, [8]);
138+
expect(snapshots.last.single.pushNotificationsEnabled, isFalse);
139+
expect(
140+
find.text('Waiting for relay confirmation; notifications may continue'),
141+
findsOneWidget,
142+
);
143+
final offSwitch = tester.widget<Switch>(find.byType(Switch));
144+
expect(offSwitch.value, isFalse);
145+
expect(offSwitch.onChanged, isNull);
146+
await tester.tap(
147+
find.byKey(const ValueKey('push-notifications-open-settings')),
148+
);
149+
await tester.pump();
150+
expect(settingsOpened, 2);
151+
relayAvailable = true;
152+
await container
153+
.read(communityListProvider.notifier)
154+
.retryPendingPushLeaseTombstone(community.id);
155+
await tester.pumpAndSettle();
156+
expect(
157+
(await storage.loadAll())
158+
.single
159+
.pushSubscriptionState
160+
.pendingTombstoneGeneration,
161+
isNull,
162+
);
163+
expect(find.text('Notifications'), findsNothing);
164+
expect(tester.takeException(), isNull);
165+
debugDefaultTargetPlatformOverride = null;
166+
});
167+
}
168+
}
169+
170+
class _Lifecycle extends AppLifecycleNotifier {
171+
@override
172+
AppLifecycleState build() => AppLifecycleState.resumed;
173+
}
174+
175+
class _Auth extends AuthNotifier {
176+
@override
177+
Future<AuthState> build() async =>
178+
const AuthState(status: AuthStatus.unauthenticated);
179+
}

0 commit comments

Comments
 (0)