Files
buzz/mobile/lib/features/settings/settings_page/notifications_section.dart
Tom Brow c4c97119c3 fix(mobile): end a refused push opt-out in a visible failure with Retry (#9854)
## Summary

Turning push notifications off could stay stuck forever. If the relay
kept rejecting the opt-out tombstone,
`setPushNotificationsEnabled(false)` swallowed the rejection, and the
push bootstrap retried the tombstone on every connect, including after
restarts. Settings showed the switch off the whole time, while the relay
still held the lease.
[docs/mobile-push-suppression.md](https://github.com/block/buzz/blob/main/docs/mobile-push-suppression.md)
(rule 29) asks for the opposite: a bounded pending state, then a clear
warning that notifications may continue, with no automatic retry, and
the relay's actual setting on the next sync.

- **Bounded pending.** An opt-out ends about 10 seconds after the tap,
across every step (capability lookup, connect, authenticate, publish).
While it runs, the row says "Turning off…" and the switch is held, so
Off, On, Off can't race the first attempt.
- **Definite vs. maybe stored.** A one-shot publish now says whether the
relay could have seen the event: a refusal, or a failure before the
EVENT went out, is definite; a lost acknowledgment after it went out is
not.
- **Refused: back to the confirmed setting.** When the relay never
stored the tombstone, the saved setting goes back to On (the confirmed
lease) and Settings shows "Could not confirm that notifications were
turned off. Notifications may continue." with a **Retry** below it. The
warning lasts for this app run; after a restart, Settings just shows On.
- **Maybe stored: kept as a guard, then read back.** When the
acknowledgment was lost, the saved opt-out stays as a guard (no lease or
enrollment is published, and the community stays in the iOS notification
snapshot), while Settings shows the confirmed On with the same warning.
On the next sync, the app reads its own lease from the relay: an
inactive lease or none confirms Off; an active one turns the switch back
On and keeps the warning. A read that fails changes nothing and is tried
again on the next connect. The read asks the relay for strong
consistency, so a lagging replica can't answer with the lease the
tombstone replaced. It fails if the active community changes while it
waits, and it settles only the opt-out it was started for, so a Retry
made during the read stays pending.
- **No flash on success.** Settings reads the community from the
community list, which changes in the same step as each opt-out result.
The active-community projection trails it by a keychain read, which
could show an accepted opt-out as failed for a frame.
- **Explicit retry only.** Retry, or turning the switch off again, makes
one attempt at a new generation. The bootstrap's automatic tombstone
retry is removed.

The warning is announced to screen readers when it appears, and Retry
has its own label. This applies to every platform: the opt-out path is
shared.

### Related issue

None found.

### Testing

- The Settings recovery journey covers a refusal restoring On with the
warning, a restart that drops the warning and makes no attempt, an
attempt that never answers (held switch, then the deadline, kept as a
guard), a restart that keeps that warning, and a Retry that succeeds. It
runs with loading, absent and failed capability, and on a small phone at
text scale 2.
- The toggle journey, with the real push bootstrap, covers a refusal (a
reconnect neither retries nor reads), an unanswered attempt and a lease
that's still active: the read on reconnect, a failed read that changes
nothing, then Off or On from what the relay holds.
- One-shot publish rows: a relay that stalls at authentication fails as
never sent; one that stalls at OK, or closes after the EVENT, fails as
maybe stored, all by the deadline.
- Lease reader rows: the exact strong-read filter, an empty answer, and
a community switch during the descriptor fetch or the query. A provider
row holds a read while a Retry fails unanswered: the old read leaves the
new opt-out pending.
- A widget test with slow secure storage checks every frame of an
accepted opt-out for the warning.
- A relay database test reads back the author's inactive replacement
lease with the filter the app uses, and the lease decryption is checked
against the member's own and forged events.

### Screenshots

After the relay refuses the opt-out (no relay push capability, so the
toggle is reachable only because push is on).

| Before | After |
| --- | --- |
| <img
src="https://raw.githubusercontent.com/block/buzz/d2e3c753b3068a68728199096bf30e45e8948af7/pr-9854--before.png"
width="300" alt="Before: switch shows off while notifications may
continue"> | <img
src="https://raw.githubusercontent.com/block/buzz/ebe70a39406cf2c2104788c4cbc2f3302ba5dbbb/pr-9854--after.png"
width="300" alt="After: switch back on, with the warning and Retry below
it"> |

At text scale 2 on a small phone, Retry sits below the warning:

<img
src="https://raw.githubusercontent.com/block/buzz/ebe70a39406cf2c2104788c4cbc2f3302ba5dbbb/pr-9854--after-large-text.png"
width="250" alt="Large text: warning readable, Retry below it">

While an opt-out attempt is in flight (new), the switch is held:

<img
src="https://raw.githubusercontent.com/block/buzz/ebe70a39406cf2c2104788c4cbc2f3302ba5dbbb/pr-9854--turning-off.png"
width="300" alt="Notifications row with the subtitle Turning off and the
switch held">

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
2026-11-25 10:41:15 -05:00

167 lines
6.6 KiB
Dart

part of '../settings_page.dart';
bool _notificationsVisible(WidgetRef ref) {
if (defaultTargetPlatform != TargetPlatform.iOS &&
defaultTargetPlatform != TargetPlatform.android) {
return false;
}
final community = ref.watch(activeCommunityProvider).value;
if (community == null) return false;
// Without a gateway an Android build never enrolls, so there is nothing to
// switch on. A saved opt-in (or an unfinished opt-out) still shows the row:
// turning it off is the only move left, and the row goes inert once off.
// (iOS keeps a saved opt-in visible the same way.)
if (defaultTargetPlatform == TargetPlatform.android &&
!Env.pushGatewayConfigured) {
return community.pushNotificationsEnabled ||
community.pushSubscriptionState.pendingTombstoneGeneration != null;
}
final setup = ref.watch(buzzPushSetupStatusProvider);
final capability = ref.watch(currentRelayPushDescriptorProvider);
return (!capability.isLoading &&
!capability.hasError &&
capability.value != null) ||
community.pushNotificationsEnabled ||
community.pushSubscriptionState.pendingTombstoneGeneration != null ||
setup.hasError;
}
class _NotificationsSection extends ConsumerWidget {
const _NotificationsSection();
@override
Widget build(BuildContext context, WidgetRef ref) {
if (!_notificationsVisible(ref)) return const SizedBox.shrink();
final active = ref.watch(activeCommunityProvider).value!;
// The list changes in the same frame as each opt-out step, while the
// active projection trails it by a storage read. Reading the list keeps a
// finished opt-out from showing as failed for a frame.
final community =
ref
.watch(communityListProvider)
.value
?.where((candidate) => candidate.id == active.id)
.firstOrNull ??
active;
final setup = ref.watch(buzzPushSetupStatusProvider);
final capability = ref.watch(currentRelayPushDescriptorProvider);
final hasCapability =
!capability.isLoading &&
!capability.hasError &&
capability.value != null;
final status = ref.watch(pushOptOutStatusProvider)[community.id];
final turningOff = status == PushOptOutStatus.turningOff;
// An opt-out the relay hasn't confirmed: one that failed in this run, or
// one an earlier run left pending.
final optOutFailed =
!turningOff &&
(status == PushOptOutStatus.failed ||
(!community.pushNotificationsEnabled &&
community.pushSubscriptionState.pendingTombstoneGeneration !=
null));
// While unconfirmed, the switch shows the relay's last confirmed setting.
final shownEnabled =
!turningOff &&
(community.pushNotificationsEnabled ||
(optOutFailed && community.pushSubscriptionState.accepted != null));
// Disabled while turning off, so a second tap can't start another opt-out
// before this one ends.
final canToggle =
!turningOff && (hasCapability || shownEnabled || optOutFailed);
final communities = ref.read(communityListProvider.notifier);
void retryOptOut() => unawaited(communities.retryPushOptOut(community.id));
void setEnabled(bool enabled) {
if (optOutFailed && !enabled) return retryOptOut();
unawaited(communities.setPushNotificationsEnabled(community.id, enabled));
}
return AppListCard(
verticalPadding: Grid.twelve,
children: [
if (setup.hasError)
AppListRow(
key: const ValueKey('push-setup-retry'),
title: 'Notification setup failed',
subtitle: 'You can keep using Buzz. Tap to retry notifications.',
onTap: () => retryBuzzPushSetup(ref),
),
ValueListenableBuilder<String?>(
valueListenable: pushUnavailableReason,
builder: (context, reason, _) => reason == null
? const SizedBox.shrink()
: const AppListRow(
key: ValueKey('push-unavailable'),
title: 'Push is unavailable on this phone',
subtitle:
'You can keep using Buzz. New messages appear when you '
'open the app.',
),
),
MergeSemantics(
key: const ValueKey('push-notifications-setting'),
child: Stack(
alignment: Alignment.centerRight,
children: [
AppListRow(
key: const ValueKey('push-notifications-enabled'),
title: 'Notifications',
// Until the relay confirms, off is not yet true.
subtitle: turningOff ? 'Turning off…' : null,
// Reserve horizontal room without letting the switch's 48dp
// layout height expand the row.
trailing: const SizedBox(width: 60),
onTap: canToggle ? () => setEnabled(!shownEnabled) : null,
),
Positioned.fill(
right: Grid.xs,
child: Align(
alignment: Alignment.centerRight,
child: Switch.adaptive(
value: shownEnabled,
onChanged: canToggle ? setEnabled : null,
),
),
),
],
),
),
if (optOutFailed)
// Announced when it appears; it sits outside the switch's merged
// semantics. The wording is the spec's (rule 29): a timeout doesn't
// prove the opt-out failed.
Semantics(
key: const ValueKey('push-opt-out-failed'),
container: true,
liveRegion: true,
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
AppListRow(
title:
'Could not confirm that notifications were turned off.',
titleColor: context.colors.error,
subtitle: 'Notifications may continue.',
),
// Below the text, not trailing it, so large text sizes keep
// the warning readable.
Align(
alignment: AlignmentDirectional.centerEnd,
child: TextButton(
onPressed: retryOptOut,
child: const Text(
'Retry',
semanticsLabel: 'Retry turning off notifications',
),
),
),
],
),
),
],
);
}
}