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 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-03-10 08:18:38 -07:00
parent e20e8c7ad7
commit 62f1a88557
1 changed files with 24 additions and 17 deletions

View File

@ -2,15 +2,16 @@ import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart'; import 'package:device_info_plus/device_info_plus.dart';
import 'package:dio/dio.dart';
import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.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 'package:logger/logger.dart';
import '../config/api_endpoints.dart'; import '../config/api_endpoints.dart';
import '../network/dio_client.dart'; import '../config/app_config.dart';
/// Platforms that map to backend PushPlatform enum. /// Platforms that map to backend PushPlatform enum.
enum PushPlatform { fcm, hms, mi, oppo, vivo } enum PushPlatform { fcm, hms, mi, oppo, vivo }
@ -37,6 +38,7 @@ class PushService {
final _log = Logger(); final _log = Logger();
final _localNotifications = FlutterLocalNotificationsPlugin(); final _localNotifications = FlutterLocalNotificationsPlugin();
late final Dio _dio;
static const _androidChannelId = 'it0_push_high'; static const _androidChannelId = 'it0_push_high';
static const _androidChannelName = 'iAgent 通知'; static const _androidChannelName = 'iAgent 通知';
@ -57,10 +59,17 @@ class PushService {
// Public API // Public API
Future<void> init({required String deviceId}) async { Future<void> init({required String deviceId, String? authToken}) async {
if (_initialized) return; if (_initialized) return;
_initialized = true; _initialized = true;
_deviceId = deviceId; _deviceId = deviceId;
_dio = Dio(BaseOptions(
baseUrl: AppConfig.production().apiBaseUrl,
headers: {
'Content-Type': 'application/json',
if (authToken != null) 'Authorization': 'Bearer $authToken',
},
));
await _initLocalNotifications(); await _initLocalNotifications();
@ -83,7 +92,7 @@ class PushService {
Future<void> unregister() async { Future<void> unregister() async {
if (_platform == null || _deviceId == null) return; if (_platform == null || _deviceId == null) return;
try { try {
await DioClient().dio.delete( await _dio.delete(
ApiEndpoints.notificationDeviceToken, ApiEndpoints.notificationDeviceToken,
data: { data: {
'platform': _platform!.name.toUpperCase(), 'platform': _platform!.name.toUpperCase(),
@ -139,26 +148,24 @@ class PushService {
// HMS (Huawei / Honor) // HMS (Huawei / Honor)
// //
// huawei_push 6.x API: // huawei_push 6.x API:
// HuaweiPush.getToken() triggers async token delivery via onTokenEvent // Push.getToken(scope) triggers async token delivery via getTokenStream
// HuaweiPush.onTokenEvent Stream<TokenEvent> where event.event == Event.ON_NEW_TOKEN // Push.getTokenStream Stream<String>
// HuaweiPush.onMessageReceivedEvent Stream<RemoteMessage> // Push.onMessageReceivedStream Stream<RemoteMessage>
Future<void> _initHms() async { Future<void> _initHms() async {
try { try {
await HuaweiPush.turnOnPush(); await Push.turnOnPush();
// Request token delivered asynchronously via onTokenEvent // Token delivered asynchronously via getTokenStream
HuaweiPush.onTokenEvent.listen((tokenEvent) { Push.getTokenStream.listen((token) {
if (tokenEvent.event == Event.ON_NEW_TOKEN) { if (token.isNotEmpty) _registerToken(PushPlatform.hms, token);
_registerToken(PushPlatform.hms, tokenEvent.result);
}
}); });
// Trigger token request // Trigger token request
HuaweiPush.getToken(''); Push.getToken('');
// Foreground messages // Foreground messages
HuaweiPush.onMessageReceivedEvent.listen((msg) { Push.onMessageReceivedStream.listen((msg) {
_showLocal( _showLocal(
title: msg.notification?.title ?? '', title: msg.notification?.title ?? '',
body: msg.notification?.body ?? '', body: msg.notification?.body ?? '',
@ -166,7 +173,7 @@ class PushService {
}); });
// Background tap // Background tap
HuaweiPush.onNotificationOpenedApp.listen((_) => _tapController.add(null)); Push.onNotificationOpenedApp.listen((_) => _tapController.add(null));
} catch (e) { } catch (e) {
// HMS init failed on this Huawei device log and do nothing. // HMS init failed on this Huawei device log and do nothing.
@ -276,7 +283,7 @@ class PushService {
Future<void> _registerToken(PushPlatform platform, String token) async { Future<void> _registerToken(PushPlatform platform, String token) async {
_log.i('[Push] Registering token [$platform]'); _log.i('[Push] Registering token [$platform]');
try { try {
await DioClient().dio.post( await _dio.post(
ApiEndpoints.notificationDeviceToken, ApiEndpoints.notificationDeviceToken,
data: { data: {
'platform': platform.name.toUpperCase(), 'platform': platform.name.toUpperCase(),