fix(mining-app): 前台回来时也上传遥测队列(大厂标准做法)

- 进入后台(paused)时立即 flush:session_end + 设备信息不丢失
- 回到前台(resumed)时立即 flush:上传被强杀遗留事件 + 新 session_start

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-03-05 20:07:11 -08:00
parent 893513ad78
commit 5e6ab25199
2 changed files with 9 additions and 0 deletions

View File

@ -70,6 +70,8 @@ class SessionManager with WidgetsBindingObserver {
_state = SessionState.foreground; _state = SessionState.foreground;
_startNewSession(); _startNewSession();
// + session_start
_telemetryService?.flushOnBackground();
} }
void _handleBackground() { void _handleBackground() {
@ -77,6 +79,8 @@ class SessionManager with WidgetsBindingObserver {
_state = SessionState.background; _state = SessionState.background;
_endCurrentSession(); _endCurrentSession();
// session_end
_telemetryService?.flushOnBackground();
} }
void _startNewSession() { void _startNewSession() {

View File

@ -290,6 +290,11 @@ class TelemetryService {
debugPrint('[Telemetry] User ID cleared'); debugPrint('[Telemetry] User ID cleared');
} }
Future<void> flushOnBackground() async {
await _uploader.uploadBatch(batchSize: 50);
debugPrint('[Telemetry] Flushed on background');
}
Future<void> pauseForLogout() async { Future<void> pauseForLogout() async {
_uploader.stopPeriodicUpload(); _uploader.stopPeriodicUpload();
await _storage.clearEventQueue(); await _storage.clearEventQueue();