1.
#pragma mark 极光推送在收到消息后,执行的方法
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Required, iOS 7 Support 获取到的数据
[JPUSHService handleRemoteNotification:userInfo];
_pushDic = [[NSMutableDictionary alloc]init];
NSDictionary *dic= userInfo [@"aps"];
[_pushDic setValue:[userInfo objectForKey:@"_j_msgid"] forKey:@"_j_msgid"];
[_pushDic setValue:userInfo [@"order_number"] forKey:@"order_number"];
[_pushDic setValue:dic[@"alert"] forKey:@"alert"];
[_pushDic setValue:dic[@"badge"] forKey:@"badge"];
[_pushDic setValue:dic[@"sound"] forKey:@"sound"];
completionHandler(UIBackgroundFetchResultNewData);
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {//这是在前台
[self creatOrderAlertView];
}else{//这是在后台
[self goToMssageViewControllerWith:_pushDic];
}
}
2.
#pragma mark 在前台接到消息后执行的办法
-(void)creatOrderAlertView{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"推送消息"
message:@"您有一条新的推送消息!"
delegate:self
cancelButtonTitle:@"取消"
otherButtonTitles:@"查看",nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
[JPUSHService resetBadge];
if (buttonIndex==1) {
[self goToMssageViewControllerWith:_pushDic];
}
}
3.
#pragma mark 极光推送后台跳转方法
- (void)goToMssageViewControllerWith:(NSDictionary*)msgDic{
//将字段存入本地,因为要在你要跳转的页面用它来判断,这里我只介绍跳转一个页面,
NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults];
[pushJudge setObject:@"push"forKey:@"push"];
[pushJudge synchronize];
NSString * targetStr = [msgDic objectForKey:@"_j_msgid"];
if (targetStr) {
MyOrderViewController * VC = [[MyOrderViewController alloc]init];
UINavigationController * Nav = [[UINavigationController alloc]initWithRootViewController:VC];//这里加导航栏是因为我跳转的页面带导航栏,如果跳转的页面不带导航,那这句话请省去。
[[self topViewController] presentViewController:Nav animated:YES completion:nil];
}
}
4.
#pragma mark 通过我下面的函数可以获取当前的controller
- (UIViewController*)topViewController{
return [self topViewControllerWithRootViewController:self.window.rootViewController];
}
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController{
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabBarController = (UITabBarController *)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}