跳到主要内容

消息推送

更新时间:2025-01-20 18:55:32

该功能依赖移动应用推送服务,需要先在控制台配置后才可以正常使用。

推送配置

// 导入推送SDK头文件
#import <RTCXPush/RTCXPush.h>

申请 APNs 的 token,需要在 AppDelegate 类的 application:didRegisterForRemoteNotificationsWithDeviceToken:中完成 token 注册。

@protocol RTCXPushProtocol <NSObject>
/// sdk初始化方法,请在 AppDelegate [application:didFinishLaunchingWithOptions:`] 回调方法中调用此API
/// @param application 应用实例,直接使用AppDelegate [application:didFinishLaunchingWithOptions:`] 回调方法的 application 参数
/// @param launchOptions 应用启动选项,直接使用AppDelegate [application:didReceiveRemoteNotification:`] 回调方法的 launchOptions 参数
- (BOOL)application:(UIApplication *_Nonnull)application didFinishLaunchingWithOptions:(NSDictionary *_Nullable)launchOptions;



/// 注册 apns 成功回调时,使用此方法通知 sdk 初始化 移动推送通道
/// @param application 应用实例,直接使用AppDelegate [application:didRegisterForRemoteNotificationsWithDeviceToken:`] 回调方法的 application 参数
/// @param deviceToken 直接使用AppDelegate [application:didReceiveRemoteNotification:`] 回调方法的 deviceToken 参数
- (void)application:(UIApplication * _Nonnull)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData * _Nonnull)deviceToken;



/// 注册 apns 成功回调时,使用此方法通知 sdk
/// @param application application 应用实例,直接使用AppDelegate [application:didFailToRegisterForRemoteNotificationsWithError:`] 回调方法的 application 参数
/// @param error 失败原因介绍。直接使用AppDelegate [application:didFailToRegisterForRemoteNotificationsWithError:`] 回调方法的 error 参数
- (void)application:(UIApplication *_Nonnull)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *_Nonnull)error;



/// 在收到 apns 推送消息,调用此 API 通知 SDK,请在 AppDelegate [application:didReceiveRemoteNotification:`] 回调方法中调用此API
/// @param application 应用实例,直接使用AppDelegate [application:didReceiveRemoteNotification:`] 回调方法的 application 参数
/// @param userInfo apns 推送消息的内容,直接使用AppDelegate [application:didReceiveRemoteNotification:`] 回调方法的 userInfo 参数
- (void)application:(UIApplication *_Nonnull)application didReceiveRemoteNotification:(NSDictionary * _Nonnull)userInfo;

@end

调用示例如下:

- (void)registerRemoteNotifications {
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {// iOS10特有
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted) {
// 点击允许
NSLog(@"注册推送成功");
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"UNNotificationSettings: %@", settings);
}];
} else {
// 点击不允许
NSLog(@"注册推送失败: %@",error);
}
}];
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 创建并设置窗口
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];

// 显示窗口
[self.window makeKeyWindow];
[self.window makeKeyAndVisible];
[RTCXPushService(RTCXPushProtocol) application:application didFinishLaunchingWithOptions:launchOptions];
[self registerRemoteNotifications];// 注册推送通知
return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[RTCXPushService(RTCXPushProtocol) application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Register Error: %@", error);
[RTCXPushService(RTCXPushProtocol) application:application didFailToRegisterForRemoteNotificationsWithError:error];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSLog(@"Pushlog 后台 :[%s][%@]",__func__,@([UIApplication sharedApplication].applicationState));
[RTCXPushService(RTCXPushProtocol) application:application didReceiveRemoteNotification:userInfo];
}

消息通知Schedule

推送消息Schedule计划相关API

@protocol RTCXPushServiceProtocol <NSObject>
/**
* 推送消息Schedule设置
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)setPnsScheduleWithReq:(nonnull RTCXPnsScheduleSetReq *)req onSuccess:(nullable RTCXPushServiceOnSuccess)onSuccess onError:(nullable RTCXPushServiceOnError)onError;
/**
* 推送消息Schedule查询
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)getPnsScheduleWithReq:(nonnull RTCXPnsScheduleGetReq *)req onSuccess:(nullable RTCXPushServiceOnSuccess)onSuccess onError:(nullable RTCXPushServiceOnError)onError;
@end

调用示例如下:

  • 设置推送消息Schedule
RTCXPushScheduleItem *scheduleItem = [[RTCXPushScheduleItem alloc] init];
scheduleItem.start = @(BeginTime);
scheduleItem.end = @(EndTime);
if (EndTime <= BeginTime) {
scheduleItem.end = @(EndTime+24*60*60);
}
scheduleItem.repeat = @(repeat);
scheduleItem.week = @(week);
scheduleItem.status = @(status);
if (!self.pnsScheduleM.scheduleList) {
self.pnsScheduleM.scheduleList = [NSMutableArray new];
}
[self.pnsScheduleM.scheduleList addObject:scheduleItem];
RTCXPnsScheduleSetReq *req = [[RTCXPnsScheduleSetReq alloc] init];
req.iotId = self.device.iotId;
req.schedule = self.pnsScheduleM;
[RTCXPushService(RTCXPushServiceProtocol) setPnsScheduleWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
[self showToast:@"设置消息Schedule成功"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.navigationController popViewControllerAnimated:YES];
});
} onError:^(NSError * _Nullable error) {
[self showToast:@"设置消息Schedule失败"];
}];
  • 查询推送消息Schedule
RTCXPnsScheduleGetReq *req = [[RTCXPnsScheduleGetReq alloc] init];
req.iotId = self.device.iotId;
[RTCXPushService(RTCXPushServiceProtocol) getPnsScheduleWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
[weakSelf.tableView xs_headerEndRefreshing];
weakSelf.pnsScheduleM = data;
if (!weakSelf.pnsScheduleM) {
weakSelf.pnsScheduleM = [[RTCXPushSchedule alloc] init];
self.pnsScheduleM.scheduleList = [NSMutableArray new];
weakSelf.pnsScheduleM.status = @(1);
weakSelf.pnsScheduleM.image = @(1);
}
NSMutableArray *modelArr = [[NSMutableArray alloc] initWithCapacity:0];
[weakSelf.pnsScheduleM.scheduleList enumerateObjectsUsingBlock:^(RTCXPushScheduleItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PnsScheduleModel *model = [[PnsScheduleModel alloc] init];
model.index = idx;
model.scheduleItem = obj;
[modelArr addObject:model];
}];
[weakSelf.tableView.hwhd_rowArray addObjectsFromArray:modelArr];
[weakSelf.tableView reloadData];
} onError:^(NSError * _Nullable error) {
[weakSelf.tableView xs_headerEndRefreshing];
[weakSelf showToast:@"查询消息Schedule失败"];
}];