Only display the challenge received notifications from the socket if the

app is in the foregroud because fcm already shows the notification when
the app is in the background.

Before this fix, when the app is in the background, it might show the
notification from the socket (when the socket is still alive in the
background) which is later overriden by the notification from fcm
resulting in a confusing experience for the user.
This commit is contained in:
Julien
2026-05-23 18:32:35 +02:00
parent 3e05ac99f0
commit fa82af9026
3 changed files with 22 additions and 10 deletions
+12 -8
View File
@@ -94,14 +94,18 @@ class ChallengeService {
);
// new incoming challenges
await Future.wait(
_current?.inward.whereNot((challenge) => prevInwardIds.contains(challenge.id)).map((
challenge,
) async {
return await notificationService.show(ChallengeNotification(challenge));
}) ??
<Future<int>>[],
);
// only display the notifications if the app is in the foregroud because fcm already
// shows notifications when the app is in the background
if (WidgetsBinding.instance.lifecycleState == AppLifecycleState.resumed) {
await Future.wait(
_current?.inward.whereNot((challenge) => prevInwardIds.contains(challenge.id)).map((
challenge,
) async {
return await notificationService.show(ChallengeNotification(challenge));
}) ??
<Future<int>>[],
);
}
}
/// Stop listening to challenge events from the server.
@@ -464,8 +464,8 @@ class ChallengeAcceptedNotification extends LocalNotification {
/// A notification for a challenge creation.
///
/// This notification is shown when a challenge is created on the server while the user is not connected to lichess (e.g., app is in background).
/// If the user is connected, challenges are handled by Websocket and a [ChallengeNotification] is shown instead.
/// This notification is shown when a challenge is created on the server while the app is in background.
/// If the app is in foreground, challenges are handled by Websocket and a [ChallengeNotification] is shown instead.
class ChallengeCreatedNotification extends LocalNotification {
const ChallengeCreatedNotification(this.challengeId, String title, String body)
: _title = title,
@@ -134,6 +134,10 @@ void main() {
),
).thenAnswer((_) => Future.value());
// notifications from socket are only displayed if app is in foreground
final binding = TestWidgetsFlutterBinding.ensureInitialized();
binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
final container = await makeContainer(
authUser: fakeAuthUser,
overrides: {
@@ -223,6 +227,10 @@ void main() {
() => notificationDisplayMock.cancel(id: any(named: 'id')),
).thenAnswer((_) => Future.value());
// notifications from socket are only displayed if app is in foreground
final binding = TestWidgetsFlutterBinding.ensureInitialized();
binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
final container = await makeContainer(
authUser: fakeAuthUser,
overrides: {