遠程推送時 , 應用可能處于下列三種狀態:
(1) . 應用開啟時 , 應用在前臺
(2) . 應用開啟時 , 應用在后臺
(3) . 應用未啟動(應用被殺死)
從蘋果APNS服務器遠程推送時:
1 . 如果應用處于 (1) 狀態 , 則不會發出聲音 , 會直接調用appDelegate的代理方法didReceiveRemoteNotification,此時如果想收到類似系統的彈窗提示,則需要自定義彈窗,提示音,振動(彈窗可以參考 : ForeNotification (本地下載))
AudioServicesPlaySystemSound(1007);//系統提示音AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//震動
2 . 如果應用處于 (2) 狀態 , 則會發出提示音, 點擊推送消息 , 則會調用appDelegate的代理方法didReceiveRemoteNotification
3 . 如果應用處于 (3) 狀態,則會發出提示音 , 點擊推送消息 , 則會開啟應用 , 在下面這個方法中會帶上launchOptions這個參數,如果實現了application:didReceiveRemoteNotification:fetchCompletionHandler:
這個方法,則會調用這個方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; if (remoteNotification) { //自定義的BOOL值,用來標記是從通知啟動的應用 self.isLaunchedByNotification = YES; }else{ } [self checkIsLaunchedByNotification]; return YES;}
收到遠程推送后 , 可以跳轉到消息界面 :
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{ NSDictionary *aps = [userInfo valueForKey:@"aps"]; NSDictionary *alert = [aps valueForKey:@"alert"]; NSString * body = alert[@"body"]; if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) { //處于前臺時 [EBForeNotification handleRemoteNotification:@{@"aps":@{@"alert":[NSString stringWithFormat:@"%@",body]}} soundID:1312]; }else{ //處于后臺時 [self gotoMessageVC]; }}#pragma mark - 檢測是否從通知欄啟動得應用- (void)checkIsLaunchedByNotification{ if (self.isLaunchedByNotification) { [self gotoMessageVC]; }}#pragma mark - 點擊了通知菜單(當應用在前臺時,收到推送,點擊了自定義的彈窗,調用的方法)- (void)clickBannerView:(NSNotification *)notification{ NSDictionary * dict = notification.object; [self gotoMessageVC];}#pragma mark - 跳轉到消息界面(點擊通知菜單/點擊通知欄啟動應用時)- (void)gotoMessageVC{ if([self.window.rootViewController isEqual:self.tabBarController]){ if([self.tabBarController.selectedViewController isKindOfClass:[UINavigationController class]]){ UINavigationController * nav = self.tabBarController.selectedViewController; if (![nav.topViewController isKindOfClass:[MessagesViewController class]]) { MessagesViewController *messageVC = [[MessagesViewController alloc] init]; messageVC.hidesBottomBarWhenPushed = YES; [nav.visibleViewController.navigationController pushViewController:messageVC animated:YES]; } } }}
新聞熱點
疑難解答