当我们的APP收到推送消息后,通常需要根据推送内容点击消息进入到指定的页面
这里讲一下收到推送消息后的处理,分为三种情况 :1.APP处于前台运行情况下
2.APP处于后台挂起情况下
3.APP未启动情况下
前两种相对好处理一点,我是在didReceiveRemoteNotification方法里接受到消息后发一个通知给MainViewController,跳转界面
具体如下:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Required, iOS 7 Support
[JPUSHService handleRemoteNotification:userInfo];
completionHandler(UIBackgroundFetchResultNewData);
// NSLog(@"%@",userInfo);
_notDic = [NSMutableDictionary dictionary];
[_notDic setObject:userInfo[@"ID"] forKey:@"myID"];
[_notDic setObject:userInfo[@"PICPATH"] forKey:@"myPic"];
//判断应用是在前台还是后台
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
//第一种情况前台运行
NSString *apnCount = userInfo[@"aps"][@"alert"];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"推送信息" message:apnCount delegate:self cancelButtonTitle:@"查看" otherButtonTitles:@"取消", nil];
alert.delegate = self;
[alert show];
}else{
//第二种情况后台挂起时
[[NSNotificationCenter defaultCenter]postNotificationName:KJPUSHNOT object:nil userInfo:_notDic];
}
}
第三种程序未启动时情况下,需要在didFinishLaunchingWithOptions方法里处理,而不能通过通知跳转页面,因为这时MainViewController还没有走viewDidLoad方法,是没办法接受通知的,具体代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
//判断是否是通过点击推送消息进入的APP
NSDictionary *resultDic = launchOptions[@"UIApplicationLaunchOptionsRemoteNotificationKey"];
if (resultDic) {//推送进入APP
self.window.rootViewController = wantVC;
}else{//正常进入APP
self.window.rootViewController = mainVC;
}
return YES;
}