## 推送功能接入(極光推送)
### 推送功能申請鏈接
[極光推送](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{
}