版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1337818
消息推送在现在的App中很常见,但同一个App中推送的消息可能有多种类型,点击推送需要跳转到不同的指定页面。
做法:
我们在接收到推送的时候发送通知,每个页面都接收通知,如果有通知就在当前页面进行页面的跳转跳转到指定页面。
如果在每个页面中都添加接收通知的代码会很麻烦,我们可以将接收通知的代码添加到基类中,这样就简单、方便了许多。可有些项目中的代码中可能没有基类,就像我们公司中的这个项目,那也没问题,我们可以为视图控制器添加一个分类,将接收通知的代码添加到分类中,再在pch文件中导入此分类。
接收推送发送通知的代码:
{
//关闭友盟自带的弹出框
UMessage setAutoAlert:NO;
UMessage didReceiveRemoteNotification:userInfo;
[NSNotificationCenter defaultCenter postNotificationName:@"pushNoti" object:nil];
}
接收通知进行页面跳转的代码,此代码在视图控制器的分类中:
{
Method m1;
Method m2;
// 运行时替换方法
m1 = class_getInstanceMethod(self, @selector(statisticsViewWillAppear:));
m2 = class_getInstanceMethod(self, @selector(viewWillAppear:));
method_exchangeImplementations(m1, m2);
m1 = class_getInstanceMethod(self, @selector(statisticsViewWillDisappear:));
m2 = class_getInstanceMethod(self, @selector(viewWillDisappear:));
method_exchangeImplementations(m1, m2);
}
{
self statisticsViewWillAppear:animated;
[MobClick beginLogPageView:NSStringFromClass(self class)];
[NSNotificationCenter defaultCenter addObserver:self selector:@selector(push) name:@"pushNoti" object:nil];
}
-(void) statisticsViewWillDisappear:(BOOL)animated
{
self statisticsViewWillDisappear:animated;
[MobClick endLogPageView:NSStringFromClass(self class)];
[NSNotificationCenter defaultCenter removeObserver:self name:@"pushNoti" object:nil];
}
NotificationVC * notiVC = [NotificationVC alloc init];
notiVC.hidesBottomBarWhenPushed = YES;
self.navigationController pushViewController:notiVC animated:YES;
}
该项目中之前的友盟统计就添加到了该分类中.
好了,本篇博客的主要内容就这些,谢谢阅读。