推送点击跳转到指定界面
集成的友盟推送,集成办法不赘述.直接上代码
1.appDelegate.m里面的方法; 2.要跳转到的控制器里面的方法
1.appDelegate.m里面的方法
//app的状态可能是未启动
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//如果程序没有运行,点击通知栏推送消息跳转到指定页面。
if(launchOptions) {
NSUserDefaults*pushJudge = [NSUserDefaultsstandardUserDefaults];
[pushJudge setObject:userInfoforKey:@"push"];
[pushJudge synchronize];
[self pushViewController];
}
}
#pragma mark--推送跳转到对应的界面
- (void)pushViewController
{
NSUserDefaults*pushJudge = [NSUserDefaults standardUserDefaults];
NSDictionary*numDic = [pushJudge objectForKey:@"push"];
NSInteger num = [numDic[@"pushMsgType"]integerValue];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
//跳转到物流消息0还是通知消息1
if(num ==0) {
BSLogisMessageController *vc = [[BSLogisMessageController alloc] init];
UINavigationController*pushNav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window.rootViewControllerpresentViewController:pushNavanimated:YEScompletion:nil];
}
else
{
BSNotificationMessageController *vc = [[BSNotificationMessageController alloc] init];
UINavigationController *pushNav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window.rootViewController presentViewController:pushNavanimated:YES completion:nil];
}
}
//接收到通知的状态是app打开的情况下,前台或者后台
//iOS10以下使用这个方法接收通知
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{
//1.接受服务端推送通知传来的值,全部在userinfo里面。
[UMessage setAutoAlert:NO];
[UMessage didReceiveRemoteNotification:userInfo];
self.userInfo= userInfo;
//2.发通知
//收到推送时程序正在前台运行,则给出一个alert,用户选择查看,跳转到指定页面
if(application.applicationState==UIApplicationStateActive) {
NSUserDefaults *pushJudge = [NSUserDefaults standardUserDefaults];
[pushJudge setObject:userInfoforKey:@"push"];
[pushJudge synchronize];
[self pushViewController];
}
//收到推送时程序在后台运行,点击通知栏中的推送消息,跳转到指定页面
if(application.applicationState!=UIApplicationStateBackground&& application.applicationState!=UIApplicationStateActive) {
NSUserDefaults *pushJudge = [NSUserDefaults standardUserDefaults];
[pushJudge setObject:userInfoforKey:@"push"];
[pushJudge synchronize];
[self pushViewController];
}
}
//iOS10新增:处理前台收到通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification*)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler{
NSDictionary * userInfo = notification.request.content.userInfo;
if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于前台时的远程推送接受
//关闭U-Push自带的弹出框
[UMessage setAutoAlert:NO];
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
}else{
}
//当应用处于前台时提示设置,需要哪个可以设置哪一个
completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
}
//iOS10新增:处理后台点击通知的代理方法
-(void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse*)response withCompletionHandler:(void(^)())completionHandler{
NSDictionary * userInfo = response.notification.request.content.userInfo;
if([response.notification.request.triggerisKindOfClass:[UNPushNotificationTrigger class]]) {
//应用处于后台时的远程推送接受
//必须加这句代码
[UMessage didReceiveRemoteNotification:userInfo];
NSUserDefaults *pushJudge = [NSUserDefaults standardUserDefaults];
[pushJudge setObject:userInfoforKey:@"push"];
[pushJudge synchronize];
[self pushViewController];
}else{
//应用处于后台时的本地推送接受
}
}
2.要跳转到的界面里的方法
//左边返回的点击方法
#pragma mark--返回
- (void)back
{
NSUserDefaults *pushJudge = [NSUserDefaults standardUserDefaults];
if(([pushJudgeobjectForKey:@"push"] !=nil)) {
//推送的返回
[self rebackToRootViewAction];
}
else
{
//正常的返回
[self.navigationController popViewControllerAnimated:YES];
}
}
#pragma mark--推送的返回
- (void)rebackToRootViewAction {
//将标示条件置空,以防通过正常情况下导航栏进入该页面时无法返回上一级页面
NSUserDefaults *pushJudge = [NSUserDefaults standardUserDefaults];
[pushJudge removeObjectForKey:@"push"];
[pushJudge synchronize];
[self dismissViewControllerAnimated:YES completion:nil];
}