今天检查自己的代码的时候发现AppDelegate中的代码太多了,并且有优化的空间,因此就优化了自己的代码,下面是主要代码:
GitHub下载地址:https://github.com/shuilanjianyue/JPushTest
PushEncapsulation.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface PushEncapsulation : NSObject
+(instancetype)shareJPushManger;
// 在应用启动的时候调用
- (void)setupWithOption:(NSDictionary *)launchingOption
appKey:(NSString *)appKey
channel:(NSString *)channel
apsForProduction:(BOOL)isProduction
advertisingIdentifier:(NSString *)advertisingId;
// 在appdelegate注册设备处调用
- (void)registerDeviceToken:(NSData *)deviceToken;
//设置角标
- (void)setBadge:(int)badge;
//获取注册ID
- (void)getRegisterIDCallBack:(void(^)(NSString *registerID))completionHandler;
//处理推送信息
- (void)handleRemoteNotification:(NSDictionary *)remoteInfo;
//处理跳转
- (void)getUserDic:(NSDictionary *)userDic;
PushEncapsulation.m
#import "PushEncapsulation.h"
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
#import "CourseEduController.h" //分类课程列表
#import "CourseDetailController.h"//课程详情
#import "RegisterController.h"//注册
#import "LoginController.h"//登录
#import "WenDaDetailController.h"//问答详情页
#import "BaseWebController.h"
#import "ScheduleDetailController.h" //课表详情
#import "MySingleController.h" //个人资料
#import "MyHomePageController.h"//个人首页
#import "FeedBackController.h" //反馈建议
#import "SettingController.h" //设置界面
#import "ReallyController.h" //实名认证
#import "MyOrderListController.h"//订单详情
#import "MyCollectListController.h"//我的订单
#import "MyScheduleController.h" //课表
#import "SearchController.h" //搜索
#import "FindController.h" //找回密码
#import "AppDelegate.h"
@interface PushEncapsulation()<JPUSHRegisterDelegate>
@end
@implementation PushEncapsulation
static PushEncapsulation *jpushManger;
+(instancetype)shareJPushManger{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
jpushManger = [[self alloc] init];
});
return jpushManger;
}
- (void)setupWithOption:(NSDictionary *)launchingOption
appKey:(NSString *)appKey
channel:(NSString *)channel
apsForProduction:(BOOL)isProduction
advertisingIdentifier:(NSString *)advertisingId{
//极光推送
//notice: 3.0.0及以后版本注册可以这样写,也可以继续用之前的注册方式
JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
// 可以添加自定义categories
// NSSet<UNNotificationCategory *> *categories for iOS10 or later
// NSSet<UIUserNotificationCategory *> *categories for iOS8 and iOS9
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) {
NSSet<UNNotificationCategory *> *categories;
entity.categories = categories;
}
else {
NSSet<UIUserNotificationCategory *> *categories;
entity.categories = categories;
}
}
[JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
//崩溃统计
[JPUSHService crashLogON];
// Required
// init Push(2.1.5版本的SDK新增的注册方法,改成可上报IDFA,如果没有使用IDFA直接传nil )
// 如需继续使用pushConfig.plist文件声明appKey等配置内容,请依旧使用[JPUSHService setupWithOption:launchOptions]方式初始化。
[JPUSHService setupWithOption:launchingOption appKey:appKey
channel:channel
apsForProduction:isProduction
advertisingIdentifier:nil];
}
- (void)registerDeviceToken:(NSData *)deviceToken {
[JPUSHService registerDeviceToken:deviceToken];
}
- (void)setBadge:(int)badge{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:badge];
[JPUSHService setBadge:badge]; //重置JPush服务器上面的badge值。如果下次服务端推送badge传"+1",则会在你当时JPush服务器上该设备的badge值的基础上+1;
}
- (void)getRegisterIDCallBack:(void (^)(NSString *))completionHandler{
[JPUSHService registrationIDCompletionHandler:^(int resCode, NSString *registrationID) {
if (resCode == 0) {
NSLog(@"registrationID获取成功:%@",registrationID);
completionHandler(registrationID);
[JPUSHService setAlias:@"user200287" completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
} seq:444444];
}
}];
}
- (void)handleRemoteNotification:(NSDictionary *)userInfo{
[JPUSHService handleRemoteNotification:userInfo];
}
#pragma mark- JPUSHRegisterDelegate
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
// Required
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[self handleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以选择设置
}
// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
// Required
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
[self handleRemoteNotification:userInfo];
}
completionHandler(UNNotificationPresentationOptionAlert);
[self getUserDic:userInfo];
// 系统要求执行这个方法
}
- (void)getUserDic:(NSDictionary *)userDic{
AppDelegate *app =(AppDelegate *) [UIApplication sharedApplication].delegate;
NSString *urls = userDic[@"url"];
UIViewController *vc = [self topVC:[UIApplication sharedApplication].keyWindow.rootViewController]; //拿到当前页面的VC
app.tab.selectedIndex = 0;
}
- (UIViewController *)topVC:(UIViewController *)rootViewController{
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tab = (UITabBarController *)rootViewController;
return [self topVC:tab.selectedViewController];
}else if ([rootViewController isKindOfClass:[UINavigationController class]]){
UINavigationController *navc = (UINavigationController *)rootViewController;
return [self topVC:navc.visibleViewController];
}else if (rootViewController.presentedViewController){
UIViewController *pre = (UIViewController *)rootViewController.presentedViewController;
return [self topVC:pre];
}else{
return rootViewController;
}
}
AppDelegate+PushEncapsulation.h
#import "AppDelegate.h"
static NSString *appKey = @"";
static NSString *channel = @"App Store";
static BOOL isProduction = NO;
@interface AppDelegate (PushEncapsulation)
-(void)JPushApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
@end
AppDelegate+PushEncapsulation.m
#import "AppDelegate+PushEncapsulation.h"
#import "PushEncapsulation.h"
@implementation AppDelegate (PushEncapsulation)
-(void)JPushApplication:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
[[PushEncapsulation shareJPushManger] setupWithOption:launchOptions appKey:appKey channel:channel apsForProduction:isProduction advertisingIdentifier:nil];
[[PushEncapsulation shareJPushManger] getRegisterIDCallBack:^(NSString *registerID){
//获取注册id
NSLog(@"%@",registerID);
}];
[[PushEncapsulation shareJPushManger] setBadge:0];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Required - 注册 DeviceToken
[[PushEncapsulation shareJPushManger] registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Required, iOS 7 Support
[[PushEncapsulation shareJPushManger] handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
[[PushEncapsulation shareJPushManger]getUserDic:userInfo];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// Required,For systems with less than or equal to iOS6
[[PushEncapsulation shareJPushManger] handleRemoteNotification:userInfo];
[[PushEncapsulation shareJPushManger]getUserDic:userInfo];
}
这样就封装好了,和极光推送有关的功能就拿出来,避免AppDelegate.m里面过于臃肿,优化了代码