您现在的位置是:首页 > 文章详情

iOS如何通过推送打开指定页面

日期:2018-04-23点击:394

众所周知推送功能已经是APP如今必不可少的一个APP功能,现在我就来介绍一下iOS如何通过推送打开指定页面。

先去 didFinishLaunchingWithOptions 方法配置消息,AppDelegate 要遵循 MPushRegisterDelegate 协议。

@interface AppDelegate () <MPushRegisterDelegate>

配置消息

MPushNotificationConfiguration *configuration = [[MPushNotificationConfiguration alloc] init];  configuration.types = MPushAuthorizationOptionsBadge | MPushAuthorizationOptionsSound | MPushAuthorizationOptionsAlert; [MobPush setupNotification:configurationdelegate:self];

MobPush 新增设置方法,添加了第二个参数:delegate,将第二个参数 delegate 设为 self

+ (void)setupNotification:(MPushNotificationConfiguration *)configuration delegate:(id <MPushRegisterDelegate>)delegate;

然后再去处理接受到的推送消息,跳转相应的页面,这里以 Demo 为例子,点击通知跳转 web 页面,先去推送创建后台配置 url = http://m.mob.com 键值对。
1

  • iOS 8 - 9 前台收到通知 后台点击通知
// iOS 8-9 前台收到通知 后台点击通知 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { if (application.applicationState == UIApplicationStateActive) { // 应用在前台 // 最好先弹出一个 Alert,如下图片,今日头条,当你在浏览新闻,应用在前台,他就会弹出一个 Alert,告知你是否查看详情 } else { // 应用在后台 // 应用在后台点击通知,直接跳转 web 页面 NSString *url = userInfo[@"url"]; if (url) { UINavigationController *nav = (UINavigationController *)self.window.rootViewController; WebViewController *webVC = [[WebViewController alloc] init]; webVC.url = url; [nav pushViewController:webVC animated:YES]; } } completionHandler(UIBackgroundFetchResultNewData); }

2
 iOS 10 之后,使用 MPushRegisterDelegate 协议的方法

// iOS 10 后台点击通知 - (void)mpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSUInteger options))completionHandler { // 应用在后台点击通知,直接跳转 web 页面 NSString *url = userInfo[@"url"]; if (url) { UINavigationController *nav = (UINavigationController *)self.window.rootViewController; WebViewController *webVC = [[WebViewController alloc] init]; webVC.url = url; [nav pushViewController:webVC animated:YES]; } } // iOS 10 前台收到通知 - (void)mpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler { // 跟上面的一样  }  

以上就是我整理的比较简单的方式啦~

原文链接:https://yq.aliyun.com/articles/584296
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章