iOS用戶點擊推送消息進入應用後自動跳轉到對應的ViewController

當iOS用戶經過點擊推送消息進入應用時,在AppDelegate的app

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

咱們能夠經過fetch

NSDictionary* remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];

得到推送的userinfo.
可是didFinishLaunchingWithOptions會比MainViewController的viewDidLoad先執行,此時確定不能present一個新的View,應該是讓MainViewController啓動後再檢查是否收到了推送,而後打開對應的視圖
若是remoteNotification不爲空,則說明用戶經過推送消息進入,那麼能夠聲明一個屬性ui

@property (nonatomic) BOOL isLaunchedByNotification;

用於標識用戶是否經過點擊通知消息進入本應用。
此時,atom

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo

必定會被調用,iOS7能夠使用spa

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

由於此方法的調用時,MainViewController已經被初始化,因此咱們已經能夠在MainViewController註冊推送消息的監聽,用於展現對應的視圖,以下:code

//訂閱展現視圖消息,將直接打開某個分支視圖
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentView:) name:@"PresentView" object:nil];
//彈出消息框提示用戶有訂閱通知消息。主要用於用戶在使用應用時,彈出提示框
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showNotification:) name:@"Notification" object:nil];

因此在AppDelegate的didReceiveRemoteNotification中能夠經過判斷isLaunchedByNotification來通知不一樣的展現方法。server

相關文章
相關標籤/搜索