移动推送是基于大数据技术的移动云服务。帮助App快速集成移动推送的功能,在实现高效、精确、实时的移动推送的同时,降低了开发成本,提高用户活跃度和留存率。
依赖 SDK | 概述 |
---|---|
API 通道 | 提供API通道能力,和基础环境配置信息。 |
配置服务
本SDK涉及的功能依赖移动应用推送服务,需要先在控制台配置后才可以正常使用。
如何配置服务请参见移动应用推送开发指南。其中,iOS应用开发环境/生产环境为必配。
说明 苹果公司于2019年9月正式发布iOS 13系统,为了不影响您正常使用移动推送功能,建议您马上更换新版本的SDK(pod ‘AlicloudPushIoT’, ‘1.9.5.5’)。
初始化
在API通道初始化成功后,再参考下面的示例代码初始化移动应用推送SDK。
#import <CloudPushSDK/CloudPushSDK.h> #import <IMSApiClient/IMSConfiguration.h> - (void)initCloudPush { //authCode设置 [CloudPushSDK setAuthCode:[[IMSConfiguration sharedInstance] authCode]]; // 基于无线保镖初始化 [CloudPushSDK asyncInitWithSecurity:^(CloudPushCallbackResult *res) { if (res.success) { NSLog(@"Push SDK init with security box success, deviceId: %@", [CloudPushSDK getDeviceId]); } else { NSLog(@"Push SDK init with security box failed, error: %@", res.error); } }]; }
使用说明
- 向苹果 APNs。
/** * 注册苹果推送,获取deviceToken用于推送 * @param application */ - (void)registerAPNS:(UIApplication *)application { [application registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes: (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [application registerForRemoteNotifications]; } /* * 苹果推送注册成功回调,将苹果返回的deviceToken上传到CloudPush服务器 */ - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { [CloudPushSDK registerDevice:deviceToken withCallback:^(CloudPushCallbackResult *res) { if (res.success) { NSLog(@"Register deviceToken success."); } else { NSLog(@"Register deviceToken failed, error: %@", res.error); } }]; } /* * 苹果推送注册失败回调 */ - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error); }
- 推送通知到来监听。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 点击通知将App从关闭状态启动时,将通知打开回执上报 // [CloudPushSDK handleLaunching:launchOptions];(Deprecated from v1.8.1) [CloudPushSDK sendNotificationAck:launchOptions]; return YES; } /* * App处于启动状态时,通知打开回调 */ - (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo { NSLog(@"Receive one notification."); // 取得APNS通知内容 NSDictionary *aps = [userInfo valueForKey:@"aps"]; // 内容 NSString *content = [aps valueForKey:@"alert"]; // badge数量 NSInteger badge = [[aps valueForKey:@"badge"] integerValue]; // 播放声音 NSString *sound = [aps valueForKey:@"sound"]; // 取得Extras字段内容 NSString *Extras = [userInfo valueForKey:@"Extras"]; //服务端中Extras字段,key是自己定义的 NSLog(@"content = [%@], badge = [%ld], sound = [%@], Extras = [%@]", content, (long)badge, sound, Extras); // iOS badge 清0 application.applicationIconBadgeNumber = 0; // 通知打开回执上报 // [CloudPushSDK handleReceiveRemoteNotification:userInfo];(Deprecated from v1.8.1) [CloudPushSDK sendNotificationAck:userInfo]; }
- 关联移动推送到某账号,参考API服务:/uc/bindPushChannel
- 取消关联移动推送到某账号,参考API服务:/uc/unbindPushChannel
#import <IMSApiClient/IMSApiClient.h> #import <IMSAuthentication/IMSAuthentication.h> #pragma mark - - (void)bindAPNSChannelWithDeviceId:(NSString *)deviceId completionHandler:(void (^)(NSError *error))completionHandler { NSString *path = @"/uc/bindPushChannel"; NSString *version = @"1.0.0"; NSDictionary *params = @{ @"deviceId": deviceId ? : @"", }; [self requestWithPath:path version:version params:params completionHandler:^(NSError *error, id data) { if (completionHandler) { completionHandler(error); } }]; } - (void)unbindAPNSChannelWithDeviceId:(NSString *)deviceId completionHandler:(void (^)(NSError *error))completionHandler { NSString *path = @"/uc/unbindPushChannel"; NSString *version = @"1.0.0"; NSDictionary *params = @{ @"deviceId": deviceId ? : @"", }; [self requestWithPath:path version:version params:params completionHandler:^(NSError *error, id data) { if (completionHandler) { completionHandler(error); } }]; } #pragma mark - - (void)requestWithPath:(NSString *)path version:(NSString *)version params:(NSDictionary *)params completionHandler:(void (^)(NSError *error, id data))completionHandler { IMSIoTRequestBuilder *builder = [[IMSIoTRequestBuilder alloc] initWithPath:path apiVersion:version params:params]; IMSRequest *request = [[builder setAuthenticationType:IMSAuthenticationTypeIoT] build]; [IMSRequestClient asyncSendRequest:request responseHandler:^(NSError *error, IMSResponse *response) { if (error == nil && response.code != 200) { NSDictionary *info = @{ @"message" : response.message ? : @"", NSLocalizedDescriptionKey : response.localizedMsg ? : @"", }; error = [NSError errorWithDomain:ServerErrorDomain code:response.code userInfo:info]; } if (completionHandler) { completionHandler(error, response.data); } }]; }
原创文章,作者:网友投稿,如若转载,请注明出处:https://www.cloudads.cn/archives/33992.html