# React Native 推送接入指南 **包名**:`@xuqm/rn-push` · **支持**:华为、小米、OPPO、vivo、荣耀、APNs(iOS) --- ## 1. 安装 ```bash yarn add @xuqm/rn-push ``` iOS 需要执行 `pod install`: ```bash cd ios && pod install ``` --- ## 2. Android 厂商推送集成 各厂商推送 SDK 需在原生 Android 层集成。在 `android/app/build.gradle` 中按需添加: ```gradle dependencies { // 华为 HMS Push implementation 'com.huawei.hms:push:6.9.0.300' // 小米 Push implementation 'com.xiaomi.mipush:sdk:5.0.6' // OPPO Push implementation 'com.heytap.msp:push:3.5.0' // vivo Push implementation 'com.vivo.push:sdk:3.0.0.4_484' // 荣耀 Push implementation 'com.hihonor.mcs:push:7.0.41.301' } ``` --- ## 3. 请求原生推送权限并注册 ```ts import { PushSDK } from '@xuqm/rn-push' // 触发原生推送注册(Android 请求厂商 token;iOS 请求 APNs 权限) await PushSDK.requestNativeRegistration() ``` --- ## 4. 监听推送 Token ```ts import { PushSDK } from '@xuqm/rn-push' // 在 App 启动时监听 token 回调,并向服务端注册 const unsubscribe = PushSDK.onPushToken(async (token, vendor) => { console.log('获取到 Token:', token, '厂商:', vendor) // 登录后调用注册接口 await PushSDK.setDeviceToken(token, vendor as PushVendor) }) // 在组件卸载时取消监听 unsubscribe() ``` --- ## 5. 手动注册 Token ```ts import { PushSDK } from '@xuqm/rn-push' import type { PushVendor } from '@xuqm/rn-push' // 登录成功后,将 token 注册到服务端 await PushSDK.registerToken('user_001', 'device_token_here', 'HUAWEI') ``` --- ## 6. 登出时注销 Token ```ts await PushSDK.unregisterToken('user_001') // 或简写 await PushSDK.logout('user_001') ``` --- ## 7. 多模块统一登录 Push 模块与 IM、Update 模块共享同一套登录态: ```ts import { XuqmSDK } from '@xuqm/rn-common' import { PushSDK } from '@xuqm/rn-push' await XuqmSDK.initialize({ appKey: 'your_app_key' }) await XuqmSDK.login({ userId: 'user_001', userSig: 'your_user_sig' }) // ↓ 登录后调用 PushSDK.initialize() 完成 token 注册 await PushSDK.initialize('user_001') ``` --- ## 8. iOS APNs 配置 在 `AppDelegate.m` 或 `AppDelegate.swift` 中: ```objc // AppDelegate.m - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { // 转发 token,由 rn-push 原生模块处理 [RCTEventEmitter ...] // 通过 Bridge 传至 JS } ``` > 使用 `PushSDK.requestNativeRegistration()` 会自动触发 iOS APNs 注册流程,无需额外原生代码(前提是 RN 0.76+ 自动链接)。 --- ## 9. 厂商渠道自动检测 `@xuqm/rn-common` 的 `detectPushVendor` 会根据 `device.brand` 自动识别厂商: | 品牌关键字 | 识别厂商 | |-----------|---------| | xiaomi / redmi | XIAOMI | | huawei | HUAWEI | | honor | HONOR | | oppo / realme | OPPO | | vivo / iqoo | VIVO | | iOS | APNS | | 其他 | FCM |