IOS集成个推总结
2017-08-03 09:47
113 查看
IOS集成个推总结
集成注意点
1 去个推后台申请app的应用
如果你们公司要求正式版和测试版都必须要能正常接收退送的话,你需要申请两个应用。因为同一个应用同一时间只能上传一种证书(生产环境证书或开发环境证书)。申请两个应用的话,一个上传生产环境证书,一个上传开发环境证书。2. 在项目中配置开启远程推送
在项目中做一个宏定义,分别对应生产环境的APPkey和开发环境的APPKey#ifdef DEBUG // 开发环境下对应的APPkey #define pushAppId #define pushAppKey #define pushAppSecret #else // 生产环境下对应的APPkey #define pushAppId #define pushAppKey #define pushAppSecret #endif
注意
当需要发布应用的时候一定要将开发状态更改为release状态
如果是Xcode8.0以上的版本,需要手动在
Capabilities中打开
Push Notifications
并且打开
Capabilities中
Background Modes下的
Background fetch和
Remote notifications
3. 在AppDelegate中集成个推代码
如果是之前创建的项目,需要手动导入UserNotifications.framework,并且在AppDelegate中带入头文件
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 #import <UserNotifications/UserNotifications.h> #endif
个推Demo集成代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // [ GTSdk ]:是否允许APP后台运行 // [GeTuiSdk runBackgroundEnable:YES]; // [ GTSdk ]:是否运行电子围栏Lbs功能和是否SDK主动请求用户定位 [GeTuiSdk lbsLocationEnable:YES andUserVerify:YES]; // [ GTSdk ]:自定义渠道 [GeTuiSdk setChannelId:@"GT-Channel"]; // [ GTSdk ]:使用APPID/APPKEY/APPSECRENT创建个推实例 [GeTuiSdk startSdkWithAppId:kGtAppId appKey:kGtAppKey appSecret:kGtAppSecret delegate:self]; // 注册APNs - custom method - 开发者自定义的方法 [self registerRemoteNotification]; return YES; } #pragma mark - 用户通知(推送) _自定义方法 /** 注册远程通知 */ - (void)registerRemoteNotification { /* 警告:Xcode8的需要手动开启“TARGETS -> Capabilities -> Push Notifications” */ /* 警告:该方法需要开发者自定义,以下代码根据APP支持的iOS系统不同,代码可以对应修改。 以下为演示代码,注意根据实际需要修改,注意测试支持的iOS系统都能获取到DeviceToken */ if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 // Xcode 8编译会调用 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionCarPlay) completionHandler:^(BOOL granted, NSError *_Nullable error) { if (!error) { NSLog(@"request authorization succeeded!"); } }]; [[UIApplication sharedApplication] registerForRemoteNotifications]; #else // Xcode 7编译会调用 UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; #endif } else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge); [[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type]; } } #pragma mark - 远程通知(推送)回调 /** 远程通知注册成功委托 */ - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSString *token = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]; token = [token stringByReplacingOccurrencesOfString:@" " withString:@""]; NSLog(@"\n>>>[DeviceToken Success]:%@\n\n", token); // [ GTSdk ]:向个推服务器注册deviceToken [GeTuiSdk registerDeviceToken:token]; } /** 远程通知注册失败委托 */ - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"\n>>>[DeviceToken Error]:%@\n\n", error.description); } #pragma mark - APP运行中接收到通知(推送)处理 - iOS 10以下版本收到推送 /** APP已经接收到“远程”通知(推送) - 透传推送消息 */ - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { // [ GTSdk ]:将收到的APNs信息传给个推统计 [GeTuiSdk handleRemoteNotification:userInfo]; // 控制台打印接收APNs信息 NSLog(@"\n>>>[Receive RemoteNotification]:%@\n\n", userInfo); completionHandler(UIBackgroundFetchResultNewData); } #pragma mark - iOS 10中收到推送消息 #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 // iOS 10: App在前台获取到通知 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { NSLog(@"willPresentNotification:%@", notification.request.content.userInfo); // 根据APP需要,判断是否要提示用户Badge、Sound、Alert completionHandler(UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert); } // iOS 10: 点击通知进入App时触发 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler { NSLog(@"didReceiveNotification:%@", response.notification.request.content.userInfo); // [ GTSdk ]:将收到的APNs信息传给个推统计 [GeTuiSdk handleRemoteNotification:response.notification.request.content.userInfo]; completionHandler(); } #endif #pragma mark - GeTuiSdkDelegate /** SDK启动成功返回cid */ - (void)GeTuiSdkDidRegisterClient:(NSString *)clientId { // [4-EXT-1]: 个推SDK已注册,返回clientId NSLog(@"\n>>[GTSdk RegisterClient]:%@\n\n", clientId); } /** SDK遇到错误回调 */ - (void)GeTuiSdkDidOccurError:(NSError *)error { // [EXT]:个推错误报告,集成步骤发生的任何错误都在这里通知,如果集成后,无法正常收到消息,查看这里的通知。 NSLog(@"\n>>[GTSdk error]:%@\n\n", [error localizedDescription]); } /** SDK收到透传消息回调 */ - (void)GeTuiSdkDidReceivePayloadData:(NSData *)payloadData andTaskId:(NSString *)taskId andMsgId:(NSString *)msgId andOffLine:(BOOL)offLine fromGtAppId:(NSString *)appId { // [ GTSdk ]:汇报个推自定义事件(反馈透传消息) [GeTuiSdk sendFeedbackMessage:90001 andTaskId:taskId andMsgId:msgId]; // 数据转换 NSString *payloadMsg = nil; if (payloadData) { payloadMsg = [[NSString alloc] initWithBytes:payloadData.bytes length:payloadData.length encoding:NSUTF8StringEncoding]; } // 控制台打印日志 NSString *msg = [NSString stringWithFormat:@"taskId=%@,messageId:%@,payloadMsg:%@%@", taskId, msgId, payloadMsg, offLine ? @"<离线消息>" : @""]; NSLog(@"\n>>[GTSdk ReceivePayload]:%@\n\n", msg); } /** SDK收到sendMessage消息回调 */ - (void)GeTuiSdkDidSendMessage:(NSString *)messageId result:(int)result { // 发送上行消息结果反馈 NSString *msg = [NSString stringWithFormat:@"sendmessage=%@,result=%d", messageId, result]; NSLog(@"\n>>[GTSdk DidSendMessage]:%@\n\n", msg); } /** SDK运行状态通知 */ - (void)GeTuiSDkDidNotifySdkState:(SdkStatus)aStatus { // 通知SDK运行状态 NSLog(@"\n>>[GTSdk SdkState]:%u\n\n", aStatus); } /** SDK设置推送模式回调 */ - (void)GeTuiSdkDidSetPushMode:(BOOL)isModeOff error:(NSError *)error { if (error) { NSLog(@"\n>>[GTSdk SetModeOff Error]:%@\n\n", [error localizedDescription]); return; } NSLog(@"\n>>[GTSdk SetModeOff]:%@\n\n", isModeOff ? @"开启" : @"关闭"); }
作者:袁俊亮技术博客
链接:http://www.jianshu.com/p/6650e4376167
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关文章推荐
- iOS-集成支付宝支付、微信支付简单总结
- IOS集成支付宝支付方法和问题的总结
- 李洪强iOS开发本人集成环信的经验总结_09_处理好友请求
- 李洪强iOS开发本人集成环信的经验总结_01环信SDK的导入
- ios 集成 支付宝 遇到的各种问题 总结
- iOS开发中集成阿里云视频上传SDK总结
- 环信iOS SKD 3.1.0集成总结
- ios 集成 支付宝 遇到的各种问题 总结
- iOS测试与集成工具总结(转载)
- iOS - (集成支付宝第三方SDK大坑总结)
- 李洪强iOS开发本人集成环信的经验总结_06_发送好友请求
- iOS测试与集成工具总结
- IOS支付宝集成总结
- IOS支付宝集成总结
- iOS开发之适配IPV6更新网络处理为AFNetworking3.x, 集成以及遇到的一些问题总结。
- 李洪强iOS开发本人集成环信的经验总结_02_基本配置
- iOS支付宝集成的小坑--集成支付宝集成总结(1)
- iOS测试与集成工具总结(转载)
- ios 微信支付集成完结--总结
- iOS ShareSDK v3.5.1集成及遇到问题总结