From f913ae60fa403072151b730220cb84dd2f01eb83 Mon Sep 17 00:00:00 2001 From: misha Date: Mon, 29 Jun 2026 12:12:53 +0300 Subject: [PATCH] feat(vide_calls): added tokens clearing --- lib/core/firebase_api.dart | 57 +++++++++++++++++++ .../menu/presentation/pages/menu_screen.dart | 7 +++ 2 files changed, 64 insertions(+) diff --git a/lib/core/firebase_api.dart b/lib/core/firebase_api.dart index 089ba23f..a371faa3 100644 --- a/lib/core/firebase_api.dart +++ b/lib/core/firebase_api.dart @@ -99,6 +99,43 @@ class FirebaseApi { } } + + Future unregisterPushTokens() async { + if (!await TokenStorage.haveTokenInStorage()) return; + try { + await unregisterPushToken(); + } catch (_) {} + if (Platform.isIOS) { + try { + await _unregisterIosVoipPushToken(); + } catch (_) {} + } + } + + Future unregisterPushToken() async { + if (!await TokenStorage.haveTokenInStorage()) return; + try { + final isHuawei = Platform.isAndroid ? await _isHuaweiDevice() : false; + final platform = Platform.isIOS ? 'ios' : (isHuawei ? 'huawei' : 'android'); + final token = (await getToken()) ?? ''; + await sl().post( + '/api/profile/notification/token/unregister', + data: { + 'platform': platform, + 'token': token, + }, + ).timeout( + const Duration(seconds: 10), + onTimeout: () { + throw TimeoutException('unregisterPushToken timeout'); + }, + ); + } catch (e, st) { + log(e.toString(), stackTrace: st); + await _errorReporter.report(e, st, origin: 'FirebaseApi'); + } + } + Future _registerIosVoipPushToken() async { try { final token = (await FlutterCallkitIncoming.getDevicePushTokenVoIP())?.trim(); @@ -124,6 +161,26 @@ class FirebaseApi { } } + Future _unregisterIosVoipPushToken() async { + try { + final token = (await FlutterCallkitIncoming.getDevicePushTokenVoIP())?.trim() ?? ''; + await sl().post( + '/api/profile/notification/token/voip/unregister', + data: { + 'token': token, + }, + ).timeout( + const Duration(seconds: 10), + onTimeout: () { + throw TimeoutException('unregisterPushToken voip timeout'); + }, + ); + } catch (e, st) { + log(e.toString(), stackTrace: st); + await _errorReporter.report(e, st, origin: 'FirebaseApi'); + } + } + Future _isHuaweiDevice() async { if (!Platform.isAndroid) return false; try { diff --git a/lib/features/menu/presentation/pages/menu_screen.dart b/lib/features/menu/presentation/pages/menu_screen.dart index 41f405fd..67017dd0 100644 --- a/lib/features/menu/presentation/pages/menu_screen.dart +++ b/lib/features/menu/presentation/pages/menu_screen.dart @@ -2,6 +2,7 @@ import 'package:dio/dio.dart'; import 'package:eatvio_app/core/app_config.dart'; import 'package:eatvio_app/core/error_reporting/error_reporter.dart'; import 'package:eatvio_app/core/const.dart'; +import 'package:eatvio_app/core/firebase_api.dart'; import 'package:eatvio_app/core/theme/custom_text_style.dart'; import 'package:eatvio_app/core/token_storage.dart'; import 'package:eatvio_app/core/utils/image_constant.dart'; @@ -166,6 +167,12 @@ class MenuScreen extends StatelessWidget { } catch (e, s) { await sl().report(e, s, origin: 'MenuScreen'); } finally { + try { + // Needs Authorization header → must run before removing token. + await sl().unregisterPushTokens(); + } catch (e, s) { + await sl().report(e, s, origin: 'MenuScreen'); + } await sl().endCall(); await sl().clear(); await TokenStorage.removeTokenInStorage();