當我們的APP收到推送消息后,通常需要根據推送內容點擊消息進入到指定的頁面
這里講一下收到推送消息后的處理,分為三種情況 :1.APP處於前台運行情況下
2.APP處於后台掛起情況下
3.APP未啟動情況下
前兩種相對好處理一點,我是在didReceiveRemoteNotification方法里接受到消息后發一個通知給MainViewController,跳轉界面
具體如下:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { // Required, iOS 7 Support [JPUSHService handleRemoteNotification:userInfo]; completionHandler(UIBackgroundFetchResultNewData); // NSLog(@"%@",userInfo); _notDic = [NSMutableDictionary dictionary]; [_notDic setObject:userInfo[@"ID"] forKey:@"myID"]; [_notDic setObject:userInfo[@"PICPATH"] forKey:@"myPic"]; //判斷應用是在前台還是后台 if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) { //第一種情況前台運行 NSString *apnCount = userInfo[@"aps"][@"alert"]; UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"推送信息" message:apnCount delegate:self cancelButtonTitle:@"查看" otherButtonTitles:@"取消", nil]; alert.delegate = self; [alert show]; }else{ //第二種情況后台掛起時 [[NSNotificationCenter defaultCenter]postNotificationName:KJPUSHNOT object:nil userInfo:_notDic]; } }
第三種程序未啟動時情況下,需要在didFinishLaunchingWithOptions方法里處理,而不能通過通知跳轉頁面,因為這時MainViewController還沒有走viewDidLoad方法,是沒辦法接受通知的,具體代碼如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch.
//判斷是否是通過點擊推送消息進入的APP NSDictionary *resultDic = launchOptions[@"UIApplicationLaunchOptionsRemoteNotificationKey"]; if (resultDic) {//推送進入APP self.window.rootViewController = wantVC; }else{//正常進入APP self.window.rootViewController = mainVC; } return YES; }