遠程推送時 , 應用可能處于下列三種狀態(tài):
(1) . 應用開啟時 , 應用在前臺
(2) . 應用開啟時 , 應用在后臺
(3) . 應用未啟動(應用被殺死)
從蘋果APNS服務器遠程推送時:
1 . 如果應用處于 (1) 狀態(tài) , 則不會發(fā)出聲音 , 會直接調(diào)用appDelegate的代理方法didReceiveRemoteNotification,此時如果想收到類似系統(tǒng)的彈窗提示,則需要自定義彈窗,提示音,振動(彈窗可以參考 : ForeNotification )
AudioServicesPlaySystemSound(1007);//系統(tǒng)提示音AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//震動
2 . 如果應用處于 (2) 狀態(tài) , 則會發(fā)出提示音, 點擊推送消息 , 則會調(diào)用appDelegate的代理方法didReceiveRemoteNotification
3 . 如果應用處于 (3) 狀態(tài),則會發(fā)出提示音 , 點擊推送消息 , 則會開啟應用 , 在下面這個方法中會帶上launchOptions這個參數(shù),如果實現(xiàn)了application:didReceiveRemoteNotification:fetchCompletionHandler:
這個方法,則會調(diào)用這個方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ NSDictionary *remoteNotification = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey]; if (remoteNotification) { //自定義的BOOL值,用來標記是從通知啟動的應用 self.isLaunchedByNotification = YES; }else{ } [self checkIsLaunchedByNotification]; return YES;}
收到遠程推送后 , 可以跳轉(zhuǎn)到消息界面 :
- (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 - 點擊了通知菜單(當應用在前臺時,收到推送,點擊了自定義的彈窗,調(diào)用的方法)- (void)clickBannerView:(NSNotification *)notification{ NSDictionary * dict = notification.object; [self gotoMessageVC];}#pragma mark - 跳轉(zhuǎn)到消息界面(點擊通知菜單/點擊通知欄啟動應用時)- (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]; } } }}
靜默推送
應用想收到靜默推送需要滿足的條件:
1.應用在前臺/后臺 (應用被殺死就收不到了)
2.應用實現(xiàn)了
application:didReceiveRemoteNotification:fetchCompletionHandler:
3.如果僅僅實現(xiàn)了application:didReceiveRemoteNotification:,
沒有實現(xiàn)application:didReceiveRemoteNotification:fetchCompletionHandler:,
應用只有在前臺時才能收到靜默推送 , 應用在后臺時,收不到靜默推送
自定義消息推送
介紹: 極光推送提供了自定義消息推送 , 這種推送只有當應用在 前臺 時才能收到 ; 當應用在 后臺/被殺死 時,這時候的自定義消息被保存下來,直到應用處于前臺時,應用才會收到.
使用場景: 當需要在前臺處理大量數(shù)據(jù)的時候,可以使用自定義消息,例如應用某個模塊需要更新了,這時候后臺發(fā)送個自定義消息,等到應用啟動了就可以自動去下載
// 使用自定義消息,需要在通知中心注冊 // 注冊通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveJPushCustomMessage:) name:kJPFNetworkDidReceiveMessageNotification object:nil];// 接收到JPush的自定義消息推送- (void)receiveJPushCustomMessage:(NSNotification *)notification { NSDictionary *userInfo = [notification userInfo]; NSString *title = [userInfo valueForKey:@"title"]; NSString *content = [userInfo valueForKey:@"content"]; NSDictionary *extra = [userInfo valueForKey:@"extras"]; }
總結(jié):
1.應用在后臺/前臺/被殺死,都可以收到普通的遠程推送
2.應用被殺死時,可以通過Background Fetch短時間喚醒應用
3.應用在后臺/前臺時,可以通過靜默推送,修改一些數(shù)據(jù)
4.自定義消息應用在
好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網(wǎng)的支持。
新聞熱點
疑難解答
圖片精選