iOS 远程推送,根据后台推送内容的不同, 跳转指定页面

我目前的需求是总体分为两类:

1:私信、关注、点赞一类,只需跳转到对应的tabbar 中的某一项

2:每日精品文章项目推送,分两个子类

(1)如果当前已经打开 文章项目页面,则直接刷新,不推出新页面

(2)如果当前未打开此页面,则push出新的文章项目页面

iOS  推送情况分为 

应用未启动的 情况:

打开应用 ,推送信息 会通过

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

此方法传递,内容在launchOptions里面, 通过控制台即可看出

应用已启动的 情况:(1)应用在前台活跃 (前台活跃 只做 页面的体型,alert,不去主动帮用 户打开新的界面)

(2)应用在后台挂起  (后台挂起转为 活跃时 调用此方法)

- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo 


结论,我们要做的就是 通过系统的两个方法拿到推送信息后:

1、 如果是从第一个方法里边获得的信息, 则 延迟0.5秒左右跳转新页面(我做推送的时候直接 跳转会出现异常异常),这种情况不需要判断当前的 controller

2、从第二种方法里边获取信息, 判断当前controller,是否需要push新的页面,或者回到tabbar 


下面是 我这边的代码,具体 信息 字段根据你们后台来定

使用到了 类别,方便统一管理

推送平台根据你们自己所使用的来配置 ()


// 接收 远程推送消息后 弹出 新的页面
// 应用未启动的 情况下点击推送消息 进入应用 触发didFinishLaunchingWithOptions 此方法, 并从字典中 获取到推送的相关消息
// 应用已经启动  处于 活跃状态 或 非活跃状态 会触发didReceiveRemoteNotification 此方法, 并从字典中 获取到推送相关消息


#import "AppDelegate+UMessage.h"
#import "AppDelegate+UMSocal.h"
#import "ArticleOrProjectDetailViewController.h"

#define UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

#define _IPHONE80_ 80000

@implementation AppDelegate (UMessage)

- (void)setUmessageWithlaunchOptions:(NSDictionary *)launchOptions {
    [UMessage setLogEnabled:YES];
    //友盟推送 key
    [UMessage startWithAppkey:@"x x x x x x" launchOptions:launchOptions];
    //注册推送功能
    [self registerRemoteMessage];
    [self didReceiveRemoteJingWhenAppDeadWithDic:launchOptions];
}

//注册远程推送
- (void)registerRemoteMessage {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE80_
    if(UMSYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))
    {
        //register remoteNotification types
        UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
        action1.identifier = @"action1_identifier";
        action1.title=@"Accept";
        action1.activationMode = UIUserNotificationActivationModeForeground;//当点击的时候启动程序
        
        UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationAction alloc] init];  //第二按钮
        action2.identifier = @"action2_identifier";
        action2.title=@"Reject";
        action2.activationMode = UIUserNotificationActivationModeBackground;//当点击的时候不启动程序,在后台处理
        action2.authenticationRequired = YES;
        //需要解锁才能处理,如果action.activationMode = UIUserNotificationActivationModeForeground;则这个属性被忽略;
        action2.destructive = YES;
        
        UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];
        categorys.identifier = @"category1";//这组动作的唯一标示
        [categorys setActions:@[action1,action2] forContext:(UIUserNotificationActionContextDefault)];
        
        UIUserNotificationSettings *userSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert
                                                                                     categories:[NSSet setWithObject:categorys]];
        [UMessage registerRemoteNotificationAndUserNotificationSettings:userSettings];
        
        [[UIApplication sharedApplication] registerForRemoteNotifications];
        
    } else{
        //register remoteNotification types
        [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge
         |UIRemoteNotificationTypeSound
         |UIRemoteNotificationTypeAlert];
    }
#else
    [UMessage registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge
     |UIRemoteNotificationTypeSound
     |UIRemoteNotificationTypeAlert];
    
#endif
    
}

- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo {
    //关闭友盟自带的弹出框
    [UMessage setAutoAlert:NO];
    [UMessage didReceiveRemoteNotification:userInfo];
    
    NSDictionary *dic = [MyHelp jsonDataFormatterWithStringSourceData:[DMDes decryptUseDES:userInfo[@"msg"] key:DMDESKEYS]] ;
    
    if ([[dic objectForKey:@"type"] isEqualToString:@"articles"] ||
        [[dic objectForKey:@"type"] isEqualToString:@"projects"]) {
        [self didReceiveRemoteJingWhenAppBackgroundWithDic:dic];
    } else {
        [self didReceiveRemoteMessageWhenAppBackgroundWithDic:userInfo];
    }
}


#pragma mark ----应用挂起的状态 时接收到每日文章项目推送
- (void)didReceiveRemoteJingWhenAppBackgroundWithDic:(NSDictionary *)dic {
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        UITabBarController *tab = (UITabBarController *)self.window.rootViewController;
        UINavigationController  *nvc = tab.selectedViewController;
        UIViewController *vc = nvc.visibleViewController;
        if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
            //防止同一界面多次 push
            if ([vc isMemberOfClass:[ArticleOrProjectDetailViewController class]]) {
                ArticleOrProjectDetailViewController *avc = (ArticleOrProjectDetailViewController *)vc;
                if ([dic[@"type"] isEqualToString:@"articles"]) {
                    avc.detailDataTpe = DetailDataTypeArticle;
                } else {
                    avc.detailDataTpe = DetailDataTypeProject;
                }
                avc.titleString = dic[@"title"];
                avc.idString = dic[@"conId"];
                avc.iconURLString = dic[@"cover"];
                [avc reloadAll];
            } else {
                ArticleOrProjectDetailViewController *avc = (ArticleOrProjectDetailViewController *)[self getWebDetailWithData:dic];
                [vc.navigationController hideBottomBarWhenPushController:avc animated:YES superController:vc];
            }
        }
    });
}

#pragma mark -----  应用挂起的状态,收到 私信 关注等
- (void)didReceiveRemoteMessageWhenAppBackgroundWithDic:(NSDictionary *)userInfo {
    NSDictionary *subDic = userInfo[@"aps"];
    NSString *str = [NSString stringWithFormat:@"%@",userInfo];
    [self.window.rootViewController.view makeToast:subDic[@"alert"]];
    if (![str isEqualToString:@"(null)"]) {   //     后台返回数据转为字符串后为(null)用此来判断
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ChatMessage" object:nil];
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"myNewRemindMessage" object:[NSString stringWithFormat:@"%@",subDic[@"badge"]]];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:@"didreceiveNoti" object:userInfo];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"messagelistrefresh" object:nil];
    
    if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive) {
        UITabBarController *tbc = (UITabBarController *)self.window.rootViewController;
        UINavigationController  *nvc = tbc.selectedViewController;
        UIViewController *vc = nvc.visibleViewController;
        [vc.navigationController popToRootViewControllerAnimated:YES];
        tbc.tabBarController.tabBar.hidden = NO;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            tbc.selectedIndex = 2;
        });
    }
}

#pragma mark ------应用从关闭的 状态下启动时
- (void)didReceiveRemoteJingWhenAppDeadWithDic:(NSDictionary *)sic {
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        if ([sic.allKeys containsObject:UIApplicationLaunchOptionsRemoteNotificationKey]) {
            NSDictionary *subDic = [sic objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
            subDic = [MyHelp jsonDataFormatterWithStringSourceData:[DMDes decryptUseDES:[subDic objectForKey:@"msg"] key:DMDESKEYS]] ;
            ArticleOrProjectDetailViewController *avc = (ArticleOrProjectDetailViewController *)[self getWebDetailWithData:subDic];
            UITabBarController *tbc = (UITabBarController *)self.window.rootViewController;
            if ([[subDic objectForKey:@"type"] isEqualToString:@"articles"] ||
                [[subDic objectForKey:@"type"] isEqualToString:@"projects"]) {
                UINavigationController *nc = tbc.selectedViewController;
                UIViewController *vc = nc.visibleViewController;
                [vc.navigationController hideBottomBarWhenPushController:avc animated:YES superController:vc];
            } else {
                tbc.selectedIndex = 2;
            }
        }
    });
}



#pragma mark ------推送详情页
- (UIViewController *)getWebDetailWithData:(NSDictionary *)dic{
    ArticleOrProjectDetailViewController *avc = [[ArticleOrProjectDetailViewController alloc] init];
    if ([dic[@"type"] isEqualToString:@"articles"]) {
        avc.detailDataTpe = DetailDataTypeArticle;
    } else {
        avc.detailDataTpe = DetailDataTypeProject;
    }
    avc.idString = dic[@"conId"];
    avc.iconURLString = dic[@"cover"];
    return avc;
}


- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {
    [self didReceiveRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [UMessage registerDeviceToken:deviceToken];
}

+ (void)addAlias {

    [UMessage addAlias:"xxxxxx" type:@"xxxxx" response:^(id responseObject, NSError *error) {
    }];
}

//注销 个人推送  注册一个空的 alias。  用于移除个人推送, 同时 文章、项目推送不影响
+ (void)removeAlias {
    [UMessage addAlias:[@"" MD5Digest] type:@"xxxxx" response:^(id responseObject, NSError *error) {
    }];
}