iOS如何通過推送打開指定頁面
眾所周知推送功能已經是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 鍵值對。
* 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);}
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{// 跟上面的一樣 }
以上就是我整理的比較簡單的方式啦~
>
推薦閱讀: