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:
parent
e20e8c7ad7
commit
62f1a88557
|
|
@ -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<void> init({required String deviceId}) async {
|
||||
Future<void> 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<void> 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<TokenEvent> where event.event == Event.ON_NEW_TOKEN
|
||||
// HuaweiPush.onMessageReceivedEvent — Stream<RemoteMessage>
|
||||
// Push.getToken(scope) — triggers async token delivery via getTokenStream
|
||||
// Push.getTokenStream — Stream<String>
|
||||
// Push.onMessageReceivedStream — Stream<RemoteMessage>
|
||||
|
||||
Future<void> _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<void> _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(),
|
||||
|
|
|
|||
Loading…
Reference in New Issue