iOS实用

iOS 接收推送消息后跳转到某个页面(适配iOS10)

2016-11-25  本文已影响2584人  Leo丶Dicaprio

视频直播项目中添加了一个新功能,接收推送后进入到主播的房间,在这里做下笔记,写在前边,项目中我使用的推送是友盟,版本是V1.4.0,适配了iOS10,下面开始。

接入友盟推送

推荐看下这篇文章,写的很详细,在这里只补充一点。在一开始做好的时候我这里有时能获取到token,有时获取不到,如果你也有这种情况,试试下面这个图中的方法

屏幕快照 2016-11-25 下午4.11.35.png

接收推送消息后跳转到某个页面

iOS10以前使用- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo这个方法接收推送消息
iOS10以后则是使用
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
这两种方法接收,这两个的区别我在代码里做了注释。

在appdelegate.h文件中生成一个协议和一个属性

//协议
@protocol getBaseurlDelegate <NSObject>
-(void)didGetPushNotifi;
@end
//属性
@property (nonatomic,strong) NSDictionary* userInfo;        //友盟推送信息,跳转到指定房间
@property (nonatomic,strong) id<getBaseurlDelegate> delegate;  //代理

在appdelegate.m中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //...其它处理
    //友盟推送跳转到指定房间
    NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if(userInfo){//推送信息
        self.userInfo = userInfo;//[userInfo copy]
    }
    
    return YES;
}


//iOS10以下使用这个方法接收通知
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    _userInfo = userInfo;
    //关闭友盟自带的弹出框
    [UMessage setAutoAlert:YES];
    [UMessage didReceiveRemoteNotification:userInfo];
    

    
//        self.userInfo = userInfo;
        //定制自定的的弹出框
        if([UIApplication sharedApplication].applicationState == UIApplicationStateActive)
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标题"
                                                                message:@"Test On ApplicationStateActive"
                                                               delegate:self
                                                      cancelButtonTitle:@"不去"
                                                      otherButtonTitles:@"去看看",nil];
    
            [alertView show];
    
        }
}

/*
        应用没启动(后台没有应用)
        应用没启动(后台没有应用)而且锁屏状态下
        didReceiveNotificationResponse
 
        应用启动了但是在后台
        didReceiveNotificationResponse
 
        应用启动了而且在应用内
        willPresentNotification
 */
//iOS10新增:处理前台收到通知的代理方法  在程序内
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{
    NSDictionary * userInfo = notification.request.content.userInfo;
    _userInfo = userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        
        //自定义弹出框
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"标题"
                                                            message:@"接收到了推送消息"
                                                           delegate:self
                                                  cancelButtonTitle:@"不去"
                                                  otherButtonTitles:@"去看看",nil];
        [alertView show];
        //应用处于前台时的远程推送接受
        //关闭友盟自带的弹出框
        [UMessage setAutoAlert:NO];
        //必须加这句代码
        [UMessage didReceiveRemoteNotification:userInfo];
    }else{
        //应用处于前台时的本地推送接受
    }
    //当应用处于前台时提示设置,需要哪个可以设置哪一个
    completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}
//iOS10新增:处理后台点击通知的代理方法   程序挂起
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    _userInfo = userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        //应用处于后台时的远程推送接受
        //必须加这句代码
        [UMessage didReceiveRemoteNotification:userInfo];
        
    }else{
        //应用处于后台时的本地推送接受
    }
    
    [self.delegate didGetPushNotifi];
}

//这里需要遵守alertview的代理,自己添加<UNUserNotificationCenterDelegate,UIAlertViewDelegate>
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    //点击去看看
    if (buttonIndex==1) {
        [self.delegate didGetPushNotifi];
    }
    
}

在viewcontroller中(我这里使用的是tab+nav),tab加载的带nav的viewcontroller

- (void)viewDidLoad {
    [super viewDidLoad];
    //...其它操作
    //友盟推送,跳转到指定房间,处理程序未启动(后台也没有挂起)
    NSString *  pushName = [[self.appDele.userInfo objectForKey:@"aps"] objectForKey:@"alert"];
    if([Common isEmptyOrNull:pushName]){
        [self getPushInfo:self.appDele.userInfo];
    }
}

#pragma mark - appdelegate中接收到了通知进入主播房间
-(void)didGetPushNotifi{
    //友盟推送,跳转到指定房间,处理程序已启动(在后台或者程序内)
    NSString *  pushName = [[self.appDele.userInfo objectForKey:@"aps"] objectForKey:@"alert"];
    if([Common isEmptyOrNull:pushName]){
        [self getPushInfo:self.appDele.userInfo];
        //
    }
}

上面代码中的getPushInfo方法是我做的其他操作,因为我这里要用字段访问网络获取数据,打开其它房间就可以在这里面写,给个例子吧。

-(void)getPushInfo:(NSDictionary *)userInfo{
        PlayLiveViewController * PLVC = [[PlayLiveViewController alloc]init];
        PLVC.hidesBottomBarWhenPushed=YES;
        PLVC.type = @"push";
        [PLVC getValue:hM];
        [self.navigationController pushViewController:PLVC animated:YES];
}

补充下,如果要是出现崩溃的情况,请检查一下后台发送的数据的格式,有时是不要错把int当做string来处理,还有就是后台发送的数据为空的情况下也会崩溃,需要做判断处理。
点关注,不迷路(个人原创)

上一篇下一篇

猜你喜欢

热点阅读