接着上一篇继续,当接到通知需要的操作
一、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,至此推送的基本事项完成,写的仓促没有准备效果图,有什么疑问直接留言就好会及时回复的 欢迎交流进步