iOS10,XCode8.1以来 推送据说各种变化 所以自然得适配。一拖再拖,今天和个推做个了断。之前用过极光 友盟。首次接触个推
,发现有区别。个推不能同时开发和生产环境共存,而且重要的是,环境证书切换得等十几分钟才生效,这一点得注意。个推有透传消息,而且根据cid.好
长话短说 进入正题。
链接文档 SDK 应该都是信手拈来
http://docs.getui.com/mobile/ios/overview/
111
2222
3333
文件夹下有两个.h两个.a文件 最后才发现我这边只用到中间两个 第一个和第四个应该是新系统的特性,也就是官方文档所说的 个推高级功能 暂且不说 先把推送功能打通。配置完成,接下来是一系列的复制粘贴,如文档所示
---------------初始化SDK并注册APNs // AppDelegate.h#import#import "GeTuiSdk.h" // GetuiSdk头文件应用// iOS10 及以上需导入 UserNotifications.framework#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0#import#endif/// 使用个推回调时,需要添加"GeTuiSdkDelegate"/// iOS 10 及以上环境,需要添加 UNUserNotificationCenterDelegate 协议,才能使用 UserNotifications.framework 的回调@interface AppDelegate : UIResponder/// 个推开发者网站中申请App时,注册的AppId、AppKey、AppSecret
#define kGtAppId @" "
#define kGtAppKey @" "
#define kGtAppSecret @" "
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 通过个推平台分配的appId、 appKey 、appSecret 启动SDK,注:该方法需要在主线程中调用
[GeTuiSdk startSdkWithAppId:kGtAppId appKey:kGtAppKey appSecret:kGtAppSecret delegate:self];
// 注册 APNs
[self registerRemoteNotification];
return YES;
}
/** 注册 APNs */
- (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] registerForRemoteNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
#endif
} else if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
UIUserNotificationType types = (UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
UIRemoteNotificationType apn_type = (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeBadge);
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:apn_type];
}
}
-----------------向个推服务器注册DeviceToken
/** 远程通知注册成功委托 */
- (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);
// 向个推服务器注册deviceToken
[GeTuiSdk registerDeviceToken:token];
}
-------------------Background Fetch 接口回调处理
-
(void)application:(UIApplication *)application
performFetchWithCompletionHandler:(void
(^)(UIBackgroundFetchResult))completionHandler {
/// Background Fetch 恢复SDK 运行
[GeTuiSdk resume];
completionHandler(UIBackgroundFetchResultNewData);
}
---------------统计APNs通知的点击数
在iOS 10 以前,为处理 APNs 通知点击事件,统计有效用户点击数,需在AppDelegate.m里的didReceiveRemoteNotification回调方法中调用个推SDK统计接口:
-
(void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void
(^)(UIBackgroundFetchResult))completionHandler {
// 将收到的APNs信息传给个推统计
[GeTuiSdk handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
对于iOS
10 及以后版本,为处理 APNs 通知点击,统计有效用户点击数,需先添加
UNUserNotificationCenterDelegate,然后在AppDelegate.m的
didReceiveNotificationResponse回调方法中调用个推SDK统计接口:
#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
------------------------获取CID信息
个推SDK初始化完成后,可以在[GeTuiSdkDelegate GeTuiSdkDidRegisterClient]回调方法中获取注册成功的ClientID(即CID):
/** SDK启动成功返回cid */
- (void)GeTuiSdkDidRegisterClient:(NSString *)clientId {
//个推SDK已注册,返回clientId
NSLog(@"\n>>>[GeTuiSdk RegisterClient]:%@\n\n", clientId);
}
好了 这会推送就应该可以了。附加客服给我的回复。最后就是打包测试了 亲测有效
第一次提笔 多多担待 有问题及时沟通!