From 62f1a8855715c9cb4a2442fc5db0b4980f7c7313 Mon Sep 17 00:00:00 2001 From: hailin Date: Tue, 10 Mar 2026 08:18:38 -0700 Subject: [PATCH] fix(flutter): fix push_service.dart compile errors for huawei_push 6.x - Add hide RemoteMessage,Importance to huawei_push import (conflicts with firebase_messaging and flutter_local_notifications) - Replace removed HuaweiPush/Event API with Push.getTokenStream/onMessageReceivedStream - Replace non-existent DioClient() with standalone Dio instance; init() now accepts optional authToken parameter Co-Authored-By: Claude Sonnet 4.6 --- it0_app/lib/core/services/push_service.dart | 41 ++++++++++++--------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/it0_app/lib/core/services/push_service.dart b/it0_app/lib/core/services/push_service.dart index 370c2d6..2b0bb10 100644 --- a/it0_app/lib/core/services/push_service.dart +++ b/it0_app/lib/core/services/push_service.dart @@ -2,15 +2,16 @@ import 'dart:async'; import 'dart:io'; import 'package:device_info_plus/device_info_plus.dart'; +import 'package:dio/dio.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/services.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; -import 'package:huawei_push/huawei_push.dart'; +import 'package:huawei_push/huawei_push.dart' hide RemoteMessage, Importance; import 'package:logger/logger.dart'; import '../config/api_endpoints.dart'; -import '../network/dio_client.dart'; +import '../config/app_config.dart'; /// Platforms that map to backend PushPlatform enum. enum PushPlatform { fcm, hms, mi, oppo, vivo } @@ -37,6 +38,7 @@ class PushService { final _log = Logger(); final _localNotifications = FlutterLocalNotificationsPlugin(); + late final Dio _dio; static const _androidChannelId = 'it0_push_high'; static const _androidChannelName = 'iAgent 通知'; @@ -57,10 +59,17 @@ class PushService { // ── Public API ───────────────────────────────────────────────────────────── - Future init({required String deviceId}) async { + Future init({required String deviceId, String? authToken}) async { if (_initialized) return; _initialized = true; _deviceId = deviceId; + _dio = Dio(BaseOptions( + baseUrl: AppConfig.production().apiBaseUrl, + headers: { + 'Content-Type': 'application/json', + if (authToken != null) 'Authorization': 'Bearer $authToken', + }, + )); await _initLocalNotifications(); @@ -83,7 +92,7 @@ class PushService { Future unregister() async { if (_platform == null || _deviceId == null) return; try { - await DioClient().dio.delete( + await _dio.delete( ApiEndpoints.notificationDeviceToken, data: { 'platform': _platform!.name.toUpperCase(), @@ -139,26 +148,24 @@ class PushService { // ── HMS (Huawei / Honor) ────────────────────────────────────────────────── // // huawei_push 6.x API: - // HuaweiPush.getToken() — triggers async token delivery via onTokenEvent - // HuaweiPush.onTokenEvent — Stream where event.event == Event.ON_NEW_TOKEN - // HuaweiPush.onMessageReceivedEvent — Stream + // Push.getToken(scope) — triggers async token delivery via getTokenStream + // Push.getTokenStream — Stream + // Push.onMessageReceivedStream — Stream Future _initHms() async { try { - await HuaweiPush.turnOnPush(); + await Push.turnOnPush(); - // Request token — delivered asynchronously via onTokenEvent - HuaweiPush.onTokenEvent.listen((tokenEvent) { - if (tokenEvent.event == Event.ON_NEW_TOKEN) { - _registerToken(PushPlatform.hms, tokenEvent.result); - } + // Token delivered asynchronously via getTokenStream + Push.getTokenStream.listen((token) { + if (token.isNotEmpty) _registerToken(PushPlatform.hms, token); }); // Trigger token request - HuaweiPush.getToken(''); + Push.getToken(''); // Foreground messages - HuaweiPush.onMessageReceivedEvent.listen((msg) { + Push.onMessageReceivedStream.listen((msg) { _showLocal( title: msg.notification?.title ?? '', body: msg.notification?.body ?? '', @@ -166,7 +173,7 @@ class PushService { }); // Background tap - HuaweiPush.onNotificationOpenedApp.listen((_) => _tapController.add(null)); + Push.onNotificationOpenedApp.listen((_) => _tapController.add(null)); } catch (e) { // HMS init failed on this Huawei device — log and do nothing. @@ -276,7 +283,7 @@ class PushService { Future _registerToken(PushPlatform platform, String token) async { _log.i('[Push] Registering token [$platform]'); try { - await DioClient().dio.post( + await _dio.post( ApiEndpoints.notificationDeviceToken, data: { 'platform': platform.name.toUpperCase(),