因为现在做的项目需要这方面的需求和功能,把遇到的问题和解决方法分享给大家,希望共同学习
首先用的是百度推送,这里就不做具体说明了
一、先是APP程序(home键)在后台时候点击推送通知跳转到任意界面
在AppDelegate中
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
//角标清零
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
在这个方法中
if (application.applicationState == UIApplicationStateActive || application.applicationState == UIApplicationStateBackground) {
做不做处理看需求,我这里暂不做处理了
}
else//跳转到特定页面。
{
//这里面的notice_type是跟后台商量好的,跳转到特定界面所需的type值
if([[userInfo objectForKey:@"notice_type"]isEqualToString:@"0001"])
{
// 取到tabbarcontroller
RootTabBarViewController *tabBarController = ( RootTabBarViewController*)self.window.rootViewController;
if(tabBarController!=nil)
{
if([self topViewController]!=nil)
{
//如果是当前控制器是我的消息控制器的话,刷新数据即可
if([[self topViewController] isKindOfClass:[SYSNotificationViewController class]])
{
SYSNotificationViewController * vc = (SYSNotificationViewController *)[self topViewController];
[vc refreshData];
return;
}
else
{
if([self navigationViewController]!=nil)
{
SYSNotificationViewController * vc = [[SYSNotificationViewController alloc]init];
vc.hidesBottomBarWhenPushed = YES;
[[self navigationViewController] pushViewController:vc animated:YES];
}
}
}
}
}
/**
获取当前活动的navigationcontroller
@return 最顶端的Controller
*/
- (UINavigationController *)navigationViewController
{
UIWindow *window = self.window;
if ([window.rootViewController isKindOfClass:[UINavigationController class]]) {
return (UINavigationController *)window.rootViewController;
} else if ([window.rootViewController isKindOfClass:[UITabBarController class]]) {
UIViewController *selectVc = [((UITabBarController *)window.rootViewController) selectedViewController];
if ([selectVc isKindOfClass:[UINavigationController class]]) {
return (UINavigationController *)selectVc;
}
}
return nil;
}
/**
获取当前活动的navigationcontroller的顶层Controller
@return 最顶端的Controller
*/
- (UIViewController *)topViewController
{
UINavigationController *nav = [self navigationViewController];
if (!nav) {
return nil;
}
return nav.topViewController;
}
二、然后APP程序杀死的时候点击推送通知跳转到任意界面
程序杀死收到推送必走下面的方法
在AppDelegate中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
NSDictionary *remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
// 如果remoteNotification不为空,代表有推送发过来,以下类似
if (remoteNotification) {
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
//infoDict
是在AppDelegate的.h文件中申明的一个不可变字典
_infoDict = remoteNotification;
}
然后在程序运行出来的首页
AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;
//notice_type作用同上
NSString * pushName = [app.infoDict objectForKey:@"notice_type"];
if(NotNilAndNull(pushName))
{
if([pushName isEqualToString:@"0001"])
{
SYSNotificationViewController * vc = [[SYSNotificationViewController alloc]init];
vc.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:vc animated:YES];
}
}