- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
在程序啓動完成後,在此方法中能夠獲得推送通知的類容,此處又分爲遠程通知和本地通知的區別。ios
遠程通知app
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
本地通知spa
UILocalNotification *localNoti = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; NSDictionary * userInfo = localNoti.userInfo;
這裏,爲了讓主界面先加載完成,通常須要延遲一小段時間後再去處理推送通知,push出相應的響應頁面等。code
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self didReceiveNotification:userInfo]; });
遠程通知get
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo;
本地通知it
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
App狀態io
UIApplicationStateActive, // 激活狀態,用戶正在使用App UIApplicationStateInactive, // 不激活狀態,用戶切換到其餘App、按Home鍵回到桌面、拉下通知中心 UIApplicationStateBackground // 在後臺運行
根據application.applicationState的狀態,判斷執行哪一種動做。class
三、檢測客戶端是否打開了接收通知的功能後臺
檢測客戶端是否打開了通知: if (IOS8) { //iOS8以上包含iOS8 UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings]; if(UIUserNotificationTypeNone != setting.types) { NSLog(@"用戶打開了通知"); }else{ [self showAlert]; } }else{ // ios7 一下 UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if(UIRemoteNotificationTypeNone != type){ NSLog(@"用戶打開了通知"); }else{ [self showAlert]; } } //用戶沒打開接受通知功能給出提示 -(void)showAlert{ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"「鳳凰雲健康」想給您發送推送通知" message:@"「通知」可能包括提醒、聲音和圖標標記。這些可在「設置」中配置。" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDefault handler:nil]; [alertController addAction:okAction]; [self presentViewController:alertController animated:YES completion:nil]; }