fix: stop push notifications after session expires without explicit logout - #248
Open
deaflynx wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes thingsboard/flutter_thingsboard_pe_app#304
Problem
When the session ends without an explicit logout (refresh token expires while the app is unused), the FCM token stays registered on the platform and the device keeps receiving alarm push notifications after the automatic logout.
Solution
push_notifications_registeredflag (TbStorage) whenever the FCM token is registered with the platform — on a freshsaveMobileSessionand wheninit()finds an existing valid session (covers installs upgrading to this version).NotificationService.handleSessionExpired(): if the flag is set, runs the full push cleanup and clears the flag. Deleting the local FCM token is what actually stops delivery — the JWT is already invalid at this point, so the server-sideremoveMobileSessioncall usually cannot succeed; subsequent pushes to the deleted token bounce withUNREGISTEREDand the platform purges the mobile session on the next delivery attempt.Login.handleUserLoaded()triggers the cleanup whenever it detects an unauthenticated client with Firebase configured. Because the trigger is the persisted flag (not the in-memory login state), it works on cold start — the main reported scenario (app killed, token expired days ago) — as well as when the expiry happens while the app is running. It is a no-op on fresh installs, after manual logout, and on subsequent logged-out launches.NotificationService.logout()now awaitsremoveMobileSessioninside a try/catch: previously the call was fire-and-forget and produced an unhandled async error exactly in the expired-token case.Limitations
Pushes delivered between the token expiry and the next app launch cannot be stopped from the client — closing that gap requires a platform-side change (tying mobile session lifetime to the auth session), which is being discussed separately.
Testing
handleSessionExpiredflag logic (test/utils/services/notification_service_test.dart) — the repo's first unit tests, mocktail-based.flutter analyze— no new findings on touched files.