iOS推送詳解(二)收到通知跳到指定頁面的處理辦法
接著上一篇繼續,當接到通知需要的操作
一、APP在前臺
實現效果,在前臺無論在那個頁面都在頂部展示一個橫條,顯示推送資訊,點選時跳到自己處理的頁面(10秒不點選自動移除這個橫條view)
直接上程式碼吧,講解一下思路,有問題留言
當接到通知,建立一個view放在window上並攜帶通知的資訊,在加一個同樣大小的control新增點選事件,點選執行我們需要操作的事項和跳轉相應頁面,方法如下
//應用在前臺 或者後臺開啟。
if (application.applicationState == UIApplicationStateActive ||application.applicationState == UIApplicationStateBackground){
self.notifyView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, KscreenWidth, 44)];
self.notifyView.tag = 10001;
_notifyView.image = [UIImage imageNamed:@"返回條.jpg"];
_notifyView.userInteractionEnabled = YES;
UIImageView *goImView = [[UIImageView alloc] initWithFrame:CGRectMake(KscreenWidth-26, 13, 11, 18)];
goImView.image = [UIImage imageNamed:@"箭頭"];
[_notifyView addSubview:goImView];
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(8, 12, KscreenWidth, 21)];
lable.textAlignment = NSTextAlignmentLeft;
NSString *alertStr = userInfo[@"aps"][@"alert"];
if (alertStr.length > 20) {
NSString *subString = [alertStr substringToIndex:19];
alertStr = [subString stringByAppendingString:@"..."];
}
lable.text = alertStr;
lable.font = [UIFont systemFontOfSize:14.0f];
[_notifyView addSubview:lable];
UIControl *control = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, KscreenWidth, 44)];
[control addTarget:self action:@selector(skipViewC) forControlEvents:UIControlEventTouchUpInside];
[_notifyView addSubview:control];
[[UIApplication sharedApplication] windows][0].hidden = NO;
UIViewController *viewVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[[UIApplication sharedApplication].keyWindow addSubview:_notifyView];
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(removeALarmView) userInfo:nil repeats:NO];
二、APP在後臺
上面直接else一下 呼叫 [self skipViewC]上面control點選事件即可。看實現方法
這裡if裡的是,直接返回了需要的資料 或者不需要返回資料只需要跳轉頁面的實現方式,將APP啟動方法裡的啟動頁宣告為全域性,在這裡直接使用跳轉到相應頁面
else裡的是,由於展示詳情頁需要展示好多內容所以返回一個id 在通過id 查詢資訊展示跳轉
- (void)skipViewC{
_launcher.selectedIndex = 0;
[self removeALarmView];
NSString *msgId = _userInfoDic[@"formid"];
if ([msgId isEqualToString:@"todo"]) {
TaskListViewController *taskListView = [[TaskListViewController alloc] init];
taskListView.title = @"待辦事項";
taskListView.isPush = @"yes";
if ([_isLogin isEqualToString:@"no"]) {
_launcher = [self.navigationController.viewControllers objectAtIndex:1];
}else{
_launcher = [self.navigationController.viewControllers objectAtIndex:0];
}
_launcher.selectedIndex = 1;
XNavigationController *currentVC = (XNavigationController *) _launcher.selectedViewController;
[currentVC pushViewController:taskListView animated:YES];
// taskListView doQuery:@"" and:0];
// [self.navigationController pushViewController:taskListView animated:YES];
// [taskListView doQuery:@"" and:0];
}else{
//請求獲取到msgAcceptPush 物件 在跳轉
NSString *svcUrl = [[AppConfig sharedInstance] urlStringOfAction:@"Msg_detailmsg"];
NSString *urlString = [svcUrl stringByAppendingString:[NSString stringWithFormat:@"?userId=%@&sendId=%@",[AppConfig sharedInstance].userId,msgId]];
GTMHTTPFetcher *fetcher = [GTMHTTPFetcher fetcherWithURLString:urlString];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
if (!error) {
NSDictionary *dict = [NSDictionary dictionaryWithXMLData:data];
id msgPushs = [dict valueForKey:@"msg"];
MsgAcceptPush *msgAcceptPush = [[MsgAcceptPush alloc] initWithDict:msgPushs];
MsgDetailTableViewController *msgDetailView = [[MsgDetailTableViewController alloc] init];
msgDetailView.title = @"通知詳情";
msgDetailView.msgId = msgAcceptPush.mid;
msgDetailView.msgType = @"accept";
msgDetailView.msgAcceptPush = msgAcceptPush;
if ([_isLogin isEqualToString:@"no"]) {
_launcher = [self.navigationController.viewControllers objectAtIndex:1];
}else{
_launcher = [self.navigationController.viewControllers objectAtIndex:0];
}
_launcher.selectedIndex = 0;
// 這裡如果程式是通過後臺進來的,那麼獲取到得這個currentVC裡是沒有子VC的需要手動建立放進去否則在詳情頁中navigationController的返回按鈕會使程式掛掉
if ([self.isbackGround isEqualToString:@"yes"]) {
XNavigationController *currentVC = (XNavigationController *) _launcher.selectedViewController;
MsgPushTableViewController *pushTableVC = [[MsgPushTableViewController alloc] init];
NSLog(@"%@",pushTableVC);
currentVC.viewControllers = @[pushTableVC];
[pushTableVC.navigationController pushViewController:msgDetailView animated:YES];
}else{
XNavigationController *currentVC = (XNavigationController *) _launcher.selectedViewController;
[currentVC pushViewController:msgDetailView animated:YES];
}
}
}];
}
}
OK,至此推送的基本事項完成,寫的倉促沒有準備效果圖,有什麼疑問直接留言就好會及時回覆的 歡迎交流進步