其他接口
KY_RegisterClientWithDeviceToken
- 功能描述:
- 将当前客户端(App)的设备令牌(Device Token)注册到 TPNS (Tencent Push Notification Service) 服务器,以便接收远程推送通知。
接口定义
+ (void)KY_RegisterClient:(NSData * _Nonnull)deviceToken
success:(void (^ _Nonnull)(void))success
failure:(void (^ _Nonnull)(NSError * _Nullable))failure;
参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
| deviceToken | NSData | 苹果推送服务 (APNs) 分配给当前设备和应用的唯一令牌。在 `AppDelegate.m` 的 `didRegisterForRemoteNotificationsWithDeviceToken:` 方法中获取。 |
| success | block | 注册成功时调用的回调块。 |
| failure | block | 注册失败时调用的回调块,包含一个 `NSError` 对象,描述失败原因。 |
代码示例
// 在 AppDelegate.m 中
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// 将 Device Token 注册到 TPNS
[KYCamera KY_RegisterClientWithDeviceToken:deviceToken
success:^{
NSLog(@"TPNS 客户端注册成功");
}
failure:^(NSError * _Nonnull error) {
NSLog(@"TPNS 客户端注册失败: %@", error.localizedDescription);
}];
}
KY_MappingWithUID
- 功能描述:
- 将一个特定设备的 UID 与当前已注册的客户端(App)进行绑定。这样,当服务器向该设备发送推送时,通知会被路由到绑定的这个 App 实例上。
接口定义
+ (void)KY_Mapping:(NSString * _Nonnull)uid
devName:(NSString * _Nonnull)devName
interval:(NSInteger)interval
success:(void (^ _Nonnull)(void))success
failure:(void (^ _Nonnull)(NSError * _Nullable))failure;
参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
| uid | string | 需要绑定的设备的唯一标识符 (UID)。 |
| devName | string | 设备的名称。这个名称会在推送通知的内容中显示,方便用户识别是哪个设备的通知。 |
| interval | int | 推送通知的接收间隔(秒)。默认值为 0,表示无间隔,即实时接收。 |
| success | block | 绑定成功时调用的回调块。 |
| failure | block | 绑定失败时调用的回调块,包含一个 `NSError` 对象。 |
代码示例
NSString *targetDeviceUID = @"..."; // 目标设备的 UID
NSString *deviceFriendlyName = @"客厅摄像头"; // 设备的友好名称
[KYCamera KY_MappingWithUID:targetDeviceUID
devName:deviceFriendlyName
interval:0
success:^{
NSLog(@"设备 UID 与 TPNS 客户端绑定成功");
[self updateDebugLog:@"KY_MappingWithUID:success"];
}
failure:^(NSError * _Nonnull error) {
NSString *errInfo = [NSString getErrInfoWithTPNSErrCode:error.code];
NSString *msg = [NSString stringWithFormat:@"KY_MappingWithUID:失败 (%@)", errInfo];
NSLog(@"%@", msg);
[self updateDebugLog:msg];
}];
KY_UnmappingWithUID
- 功能描述:
- 解除设备 UID 与当前客户端(App)的绑定关系。解绑后,该 App 将不再接收来自该设备的推送通知。
接口定义
+ (void)KY_Unmapping:(NSString * _Nonnull)uid
success:(void (^ _Nonnull)(void))success
failure:(void (^ _Nonnull)(NSError * _Nullable))failure;
参数说明
| 参数 | 类型 | 说明 |
|---|---|---|
| uid | string | 需要解绑的设备的唯一标识符 (UID)。 |
| success | block | 解绑成功时调用的回调块。 |
| failure | block | 解绑失败时调用的回调块,包含一个 `NSError` 对象。 |
代码示例
NSString *targetDeviceUID = @"..."; // 需要解绑的设备的 UID
[KYCamera KY_UnmappingWithUID:targetDeviceUID
success:^{
NSLog(@"设备 UID 与 TPNS 客户端解绑成功");
[self updateDebugLog:@"KY_UnmappingWithUID:success"];
}
failure:^(NSError * _Nonnull error) {
NSString *errInfo = [NSString getErrInfoWithTPNSErrCode:error.code];
NSString *msg = [NSString stringWithFormat:@"KY_UnmappingWithUID:失败 (%@)", errInfo];
NSLog(@"%@", msg);
[self updateDebugLog:msg];
}];
KYPushError
- 功能描述:
- 定义了与 TPNS 操作相关的错误码枚举,用于标识在注册或绑定过程中可能出现的具体错误。
枚举定义
typedef NS_ENUM(NSInteger, KYPushError) {
KYPushError_NoToken = -1,
KYPushError_BadServerResponse = -2,
KYPushError_NoParam = -3,
KYPushError_ParamsNoCmd = -4,
KYPushError_ParamsNoUID = -5,
KYPushError_ParamsNoDeviceName = -6,
KYPushError_ParamsNoSound = -7,
KYPushError_ParamsNoFormat = -8,
KYPushError_ParamsNoWhiteBlock = -9,
// ... 可能还有其他错误码
};
错误码说明
| 枚举值 | 枚举常量 | 说明 |
|---|---|---|
| -1 | `KYPushError_NoToken` | 注册客户端时,`deviceToken` 参数为空。 |
| -2 | `KYPushError_BadServerResponse` | 服务器响应错误,可能是请求的 URL 不正确或服务器内部问题。 |
| -3 | `KYPushError_NoParam` | 发送到服务器的 URL 请求中缺少必要的参数。 |
| -4 | `KYPushError_ParamsNoCmd` | URL 参数中缺少 `cmd` 关键字。 |
| -5 | `KYPushError_ParamsNoUID` | URL 参数中缺少 `uid` 关键字(在绑定操作中)。 |
| -6 | `KYPushError_ParamsNoDeviceName` | URL 参数中缺少 `device name` 关键字(在绑定操作中)。 |
| -7 | `KYPushError_ParamsNoSound` | URL 参数中缺少 `sound` 关键字(通常用于推送通知设置)。 |
| -8 | `KYPushError_ParamsNoFormat` | URL 参数中缺少 `format` 关键字。 |
| -9 | `KYPushError_ParamsNoWhiteBlock` | 操作的 UID 不在服务器的白名单中,因此被拒绝。 |
