# Flutter 推送接入指南

**模块**：`xuqm_flutter_push` · **支持**：华为、小米、OPPO、vivo、荣耀（Android）、APNs（iOS）

---

## 1. 安装

在 `pubspec.yaml` 中添加：

```yaml
dependencies:
  xuqm_flutter_push:
    git:
      url: https://xuqinmin.com/xuqinmin12/XuqmGroup-FlutterSDK.git
      ref: v0.2.0
      path: packages/push
```

---

## 2. Android 厂商推送集成

在 `android/app/build.gradle` 中按需添加厂商 SDK：

```gradle
dependencies {
    // 华为 HMS Push
    implementation 'com.huawei.hms:push:6.9.0.300'
    // 小米 Push（其他厂商同理）
}
```

---

## 3. 请求原生推送注册

```dart
import 'package:xuqm_flutter_push/xuqm_flutter_push.dart';

// 触发原生推送注册（Android: 厂商 token；iOS: APNs 权限）
await XuqmPushSdk.requestNativeRegistration();
```

---

## 4. 监听推送 Token

```dart
import 'package:xuqm_flutter_push/xuqm_flutter_push.dart';

// 监听 token 回调（广播 Stream，可多处监听）
final subscription = XuqmPushSdk.onPushToken.listen((event) async {
  final token = event['token'] ?? '';
  final vendor = event['vendor'] ?? '';
  print('获取到 Token: $token, 厂商: $vendor');

  // 登录后注册到服务端
  final push = XuqmPushSdk();
  await push.registerToken(
    'user_001',
    token,
    vendor: vendor,
    platform: Platform.isIOS ? 'IOS' : 'ANDROID',
  );
});

// 页面销毁时取消监听
subscription.cancel();
```

---

## 5. 手动注册 Token

```dart
import 'package:xuqm_flutter_push/xuqm_flutter_push.dart';
import 'dart:io';

final push = XuqmPushSdk();

await push.registerToken(
  'user_001',       // userId
  'device_token',   // token
  vendor: 'HUAWEI',
  platform: Platform.isIOS ? 'IOS' : 'ANDROID',
);
```

---

## 6. 登出时注销 Token

```dart
import 'package:xuqm_flutter_common/xuqm_flutter_common.dart' as common;

final push = XuqmPushSdk();
final deviceId = await common.apiRequest('/api/device/id');

await push.unregisterToken('user_001', deviceId: deviceId);
```

---

## 7. 检测厂商

```dart
final vendor = await XuqmPushSdk.detectVendor();
print('当前推送厂商: $vendor'); // e.g. 'HUAWEI', 'XIAOMI', 'APNS'
```

---

## 8. 多模块统一登录

```dart
import 'package:xuqm_flutter_sdk/xuqm_flutter_sdk.dart';
import 'package:xuqm_flutter_push/xuqm_flutter_push.dart';

// 初始化 SDK
await XuqmSDK.initialize(XuqmInitOptions(appKey: 'your_app_key'));

// 请求原生注册，监听 token 后手动上报
await XuqmPushSdk.requestNativeRegistration();
XuqmPushSdk.onPushToken.listen((event) async {
  final push = XuqmPushSdk();
  await push.registerToken('user_001', event['token']!);
});
```

---

## 9. iOS APNs 配置

在 Xcode 中开启 Push Notifications 能力（Signing & Capabilities → + Capability → Push Notifications）。

`Info.plist` 添加（如需后台推送）：

```xml
<key>UIBackgroundModes</key>
<array>
  <string>remote-notification</string>
</array>
```
