极光推送精准跳转及简化AppDelegate代码(分类方法)

之前项目集成极光推送,遇到了一些问题,现在闲下来,总结一下分享给大家。

我在这里主要分享的是跳转的实现和优化AppDelegate.h里的代码。其他的集成和什么的上已经有很多的大神发表过了,你们随便找找就可以了。

直接入正题,平时我们接受到远程推送的时候,点击消息分两种情况。

一. 程序没杀死的状态下,走的是以下极光的这个代理方法。

iOS 9 之后:
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler ;


iOS 9 之前:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

二. 程序已经杀死的状态下,走的是appDelegate的launchOptions方法。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

所以跳转处理要处理要在这两个地方做处理,因为我项目是做push跳转的,在两个地方做跳转处理的时候,要做区分。
在APP被杀死的情况下,点击远程推送,因为主界面没初始化完成,要做延时处理。这里用了一个笨方法。延时调用,具体时间要看你们APP的启动速度。


 [self performSelector:@selector(pushToTargetVCWithdentifier:) withObject:receiveReuserInfo afterDelay:1.0];

下面的是整个流程的代码实现


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  //其他逻辑代码省略.......

  //注册消息处理函数的处理方法
 JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
 entity.types=JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;

    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    [JPUSHService setupWithOption: launchOptions appKey:@"极光推送的Key"
                                 channel: @"iPhone"
                 apsForProduction: 0
            advertisingIdentifier: nil];

// 跳转的逻辑代码
// 如果是点击消息推送启动APP
if(receiveReuserInfo && [[NSUserDefaults standardUserDefaults] objectForKey:kXSUserID] ){
  
    [self performSelector:@selector(pus hToTargetVCWithdentifier:) withObject:receiveReuserInfo afterDelay:1.0];// 1.0是延时跳    转,具体的参考你们APP的启动速度
    }
    return YES;
}

#pragma mark - iOS7.0 later notification suport
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    [JPUSHService handleRemoteNotification:userInfo];
    if ([[UIDevice currentDevice].systemVersion floatValue]<10.0 && application.applicationState >=1 ) {
        NSLog(@"ios 7.0 later后台接收远程推送");
        [self pushToTargetVCWithdentifier:userInfo];
    }else{
        NSLog(@"ios 7.0 later前台接收远程推送");
    }
    completionHandler(UIBackgroundFetchResultNewData);
}

#pragma mark - JPUSHRegisterDelegate iOS10.0 later suport
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    
    NSDictionary * userInfo        = response.notification.request.content.userInfo;
    UNNotificationRequest *request = response.notification.request;                             // 收到推送的请求
    UNNotificationContent *content = request.content;                                                    // 收到推送的消息内容
    UNNotificationSound   *sound   = content.sound;                                                         // 推送消息的声音
    NSNumber *badge                = content.badge;                                                              // 推送消息的角标
    NSString *body                 = content.body;                                                                      // 推送消息体
    NSString *subtitle             = content.subtitle;                                                                    // 推送消息的副标题
    NSString *title                = content.title;                                                                             // 推送消息的标题

    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]  ) {
        [JPUSHService handleRemoteNotification:userInfo];

        if([UIApplication sharedApplication].applicationState >= 1){
  
                [self pushToTargetVCWithdentifier:userInfo];                                                          // 跳转处理
        }
    }else{
        NSLog(@"iOS10 收到本地通知");
    }
       completionHandler();  // 系统要求执行这个方法
}

 // 这里做Push跳转的实现
- (void)pushToTargetVCWithdentifier:(NSDictionary *)notifInfoDict {
    
    XSTabBarController *tabVC            = (XSTabBarController *)self.window.rootViewController;
    tabVC.select = 2;  
    XSBaseNavigationController *navVC    = tabVC.selectedViewController;
    //初始化完成,跳转到你想要的目标界面
   }

接下来分类实现,我们是把消息推送在APPDelegate的代码抽离出来,单独一个分类处理,这样APPDelegate中的代码就不会那么臃肿。创建APPDelegate分类,把消息推送的相关代码移动到分类中,但是需要到APPDelegate中的方法时候,我们公开方法出来就可以了,切记不要重写APPDelegate中的其他公共方法,因为分类的优先级大于原来的类, 重新回覆盖原来的方法,这样你的公共方法在APPDelegate中就不生效。

推送分类的JPushCategory.h文件实现相关代码
#import "AppDelegate.h"
#import "JPUSHService.h"
@interface AppDelegate (JPushCategory)// 成为代理

// 在AppDelegate实现的代码
/* 注册远程推送 */
-(void)setupRemoteNotificationWithlunchInfo:(NSDictionary *)launchOptions;

/* 移除角标 */
- (void)showBadgeCount ;

@end

之后再推送分类的JPushCategory.m文件实现相关代码


#import "AppDelegate+JPushCategory.h"

#import "XSJpushManager.h"
#import "JPUSHService.h"
#import 
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import 
#import "EBForeNotification.h"
#import "XSImDBManager.h"
#import "XSBaseNavigationController.h"
#endif

@implementation AppDelegate (JPushCategory)
-(void)setupRemoteNotificationWithlunchInfo:(NSDictionary *)launchOptions {
    [self registerNotificationWith:launchOptions]; // 注册远程推送
// 如果是点击消息推送启动APP
if(receiveReuserInfo && [[NSUserDefaults standardUserDefaults] objectForKey:kXSUserID] ){
  
        [self performSelector:@selector(pus hToTargetVCWithdentifier:) withObject:receiveReuserInfo afterDelay:1.0];// 1.0是延时跳转,具体的参考你们APP的启动速度
    }
}

- (void)registerNotificationWith:(NSDictionary *)launchOptions{
    
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    
    
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    [JPUSHService setupWithOption: launchOptions appKey:JPushKey
                          channel: JPushChannel
                 apsForProduction: JPushProduction
            advertisingIdentifier: nil];
    
}

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    [JPUSHService registerDeviceToken:deviceToken]; //注册通知 DeviceToken
    
}

#pragma mark - iOS7.0 later notification suport
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
    [JPUSHService handleRemoteNotification:userInfo];
    if ([[UIDevice currentDevice].systemVersion floatValue]<10.0 && application.applicationState >=1 ) {
        NSLog(@"ios 7.0 later后台接收远程推送");
        [self pushToTargetVCWithdentifier:userInfo];
        
    }else{
        NSLog(@"ios 7.0 later前台接收远程推送");
        
    }
    completionHandler(UIBackgroundFetchResultNewData);
}


#pragma mark - JPUSHRegisterDelegate iOS10.0 later suport
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }else {
        NSLog(@"iOS10 前台收到本地通知");
        if ([UIApplication sharedApplication].applicationState < 1) {
            //NSDictionary *noti = @{@"aps":@{@"alert":notification.request.content.body}};
            //[EBForeNotification handleRemoteNotification:noti soundID:1312 isIos10:YES];
        }
    }
    completionHandler(UNNotificationPresentationOptionBadge); // 需要执行这个方法,选择是否提醒用户,有Badge、Sound、Alert三种类型可以设置
}


- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    
    NSDictionary * userInfo        = response.notification.request.content.userInfo;
    UNNotificationRequest *request = response.notification.request;                           // 收到推送的请求
    UNNotificationContent *content = request.content;                                                   // 收到推送的消息内容
    UNNotificationSound   *sound   = content.sound;                                                       // 推送消息的声音
    NSNumber *badge                = content.badge;                                                             // 推送消息的角标
    NSString *body                 = content.body;                                                                    // 推送消息体
    NSString *subtitle             = content.subtitle;                                                                // 推送消息的副标题
    NSString *title                = content.title;                                                                           // 推送消息的标题
    
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]  ) {
        [JPUSHService handleRemoteNotification:userInfo];
        [self pushToTargetVCWithdentifier:userInfo];                                                              // 跳转处理
        
    }else{
        NSLog(@"iOS10 收到本地通知")
    }
    completionHandler();                                                                                                            // 系统要求执行这个方法
}

#endif


// 消息推送跳转处理
- (void)pushToTargetVCWithdentifier:(NSDictionary *)notifInfoDict {
 
}

- (void)showBadgeCount{
    
    [JPUSHService setBadge: 0];
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; // 极光推送角标处理
}

// 在AppDelegate中实现的代码


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    /*注册推送*/
    [self setupRemoteNotificationWithlunchInfo:launchOptions];
    
    return YES;
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    [application cancelAllLocalNotifications];
    [self showBadgeCount];
}


- (void)applicationWillResignActive:(UIApplication *)application {
    [self showBadgeCount];
}

最后,第一写的比较多,文章也借鉴了很多文章,不过时间太久,找不到链接了,在这里谢谢大神们的分享。

你可能感兴趣的:(极光推送精准跳转及简化AppDelegate代码(分类方法))