## 推送功能接入(极光推送)
### 推送功能申请链接
[极光推送](https://www.jiguang.cn/push?source=bdjj#B_vid=10699408370535568667)
### 接入教程
[iOS极光推送SDK集成指南](https://docs.jiguang.cn/jpush/client/iOS/ios_guide_new/)
[证书创建指南](https://docs.jiguang.cn/jpush/client/iOS/ios_cer_guide/) ***(注意:生成证书之后不要忘记双击添加到Xcode程序中哦!)
### URL Schemes及AppKey等配置
[设置第三方AppKey等](设置第三方AppKey等.md)
### 代码
##### AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//启动极光推送
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types =JPAuthorizationOptionAlert|JPAuthorizationOptionBadge | JPAuthorizationOptionSound;
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
#endif
}else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
//可以添加自定义categories
[JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge |
UIUserNotificationTypeSound |
UIUserNotificationTypeAlert)
categories:nil];
} else {
//categories 必须为nil
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wdeprecated-declarations"
[JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound) categories:nil];
#pragma clang diagnostic pop
}
//Required
// 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
[JPUSHService setupWithOption:launchOptions appKey:JPUSHKey
channel:channel
apsForProduction:isProduction
advertisingIdentifier:nil];
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
if (resCode == 0) {
NSLog(@"registrationID获取成功:%@",registrationID);
}else {
NSLog(@"registrationID获取失败");
}
}];
if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
action.title = @"查看消息";
action.identifier = @"action1";
action.activationMode = UIUserNotificationActivationModeForeground;
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
category.identifier = @"alert";
[category setActions:@[action] forContext:UIUserNotificationActionContextDefault];
UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:[NSSet setWithObjects:category, nil]];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wdeprecated-declarations"
UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
#pragma clang diagnostic pop
}
return YES;
}
#pragma mark - 容联云IM和极光的推送
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
NSLog(@"推送的内容:%@",notificationSettings);
[application registerForRemoteNotifications];
}
#pragma mark - 当程序处于前台工作时,收到消息推送会调用
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (application.applicationState == UIApplicationStateActive) {
[JPUSHService handleRemoteNotification:userInfo];
}
}
#pragma mark - 当程序处于后台运行时 收到消息推送会调用这个方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
self.callid = nil;
NSString *userdata = [userInfo objectForKey:@"c"];
NSLog(@"远程推送userdata:%@",userdata);
if (userdata) {
NSDictionary*callidobj = [NSJSONSerialization JSONObjectWithData:[userdata dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"远程推送callidobj:%@",callidobj);
if ([callidobj isKindOfClass:[NSDictionary class]]) {
self.callid = [callidobj objectForKey:@"callid"];
}
}
NSLog(@"远程推送 callid=%@",self.callid);
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
}
#pragma mark 将得到的deviceToken传给SDK
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
//极光
[JPUSHService registerDeviceToken:deviceToken];
}
- (void)setJPushCallBack {
NSLog(@"setJPushCallBack");
}
#pragma mark 注册deviceToken失败;此处失败,与SDK无关,一般是您的环境配置或者证书配置有误
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
// NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
NSLog(@"deviceToken获取失败:%@",error);
}
#pragma mark - JPUSH的代理方法
//当程序在前台时,收到推送弹出的通知
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler{
//NSDictionary *userInfo = notification.request.content.userInfo;
}
//当程序关闭时,通过点击推送弹出的通知
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
}