標籤:ios 推送
iOS推送小結
(吐槽,md的代碼編輯功能不知道是不會用還是確實不好用)
1.推送配置1.1認證配置
請自行穀百.
1.2註冊推送
//代碼來源:環信Demo //In method application:(UIApplication *)application didFinishLaunchingWithOptions: UIApplication *application = [UIApplication sharedApplication];//註冊APNSif([application respondsToSelector:@selector(registerUserNotificationSettings:)]){ UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil]; [application registerUserNotificationSettings:settings];}#if !TARGET_IPHONE_SIMULATORif ([application respondsToSelector:@selector(registerForRemoteNotifications)]) { [application registerForRemoteNotifications];}else{ UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];}#endif
registerUserNotificationSettings iOS8.0以後使用該方法來為app註冊推送許可權,第一次安裝app時會彈出對話方塊提示使用者是否允許接收推送訊息.多次調用該方法來註冊UIUserNotificationSettings會導致上一次的設定失效.
registerForRemoteNotifications iOS8.0以後在使用registerUserNotificationSettings方法註冊推送許可權後,還需要使用該方法開啟遠程推送許可權(推送分為本地推送和遠程推送,本地推送在此不做描述).該方法會回調application:didRegisterForRemoteNotificationsWithDeviceToken(在這個方法中需要將得到的deviceToken傳給自己的推送伺服器): 或者application:didFailToRegisterForRemoteNotificationsWithError: 兩個方法之一.
**registerForRemoteNotifications**iOS3.0-iOS8.0以前註冊推送的方法.
注意:上面的代碼之所以判斷是否為模擬器是因為模擬器是沒有DeviceToken的,所以註冊遠程推送沒有意義,推送原理會在文章末尾簡介.
特別地,在iOS8中,在使用registerUserNotificationSettings:註冊推送許可權時增加了一個categories參數,可以用來自訂增加一些對推送訊息的處理行為,範例程式碼如下:
UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];acceptAction.identifier = @"acceptAction";acceptAction.title = @"搶單";acceptAction.activationMode = UIUserNotificationActivationModeForeground;UIMutableUserNotificationCategory *categorys = [[UIMutableUserNotificationCategory alloc] init];categorys.identifier = @"rush";NSArray *actions = @[acceptAction];[categorys setActions:actions forContext:UIUserNotificationActionContextMinimal]; UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:[NSSet setWithObjects:categorys, nil]]; [application registerUserNotificationSettings:settings];
此外,在進行推送時,需要在推送訊息內增加一個category的key,value為rush,這樣用戶端在收到訊息後下拉或者在鎖定畫面左滑會出現一個搶單按鈕.
處理category訊息可再appdelegate的如下方法中進行:
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void (^)())completionHandler{ NSLog(@"%@----%@",identifier,userInfo); UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"搶單成功" message:nildelegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil]; [alert show]; [[NSNotificationCenter defaultCenter] postNotificationName:@"rushSuccess" object:nil]; //處理完訊息,最後一定要調用這個代碼塊 completionHandler();
}
1.3處理推送
如果是程式正在運行或者說程式正在後台,那麼這個時候處理推送訊息的工作都是在:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo 或者:-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler中完成。
如果應用程式沒有啟動:
- 帶category的訊息在點擊相應的action後會直接進入application:(UIApplication *)application handleActionWithIdentifier:方法中進行處理,
- 不帶category的訊息在點擊訊息進入app後,會先調用application didReceiveRemoteNotification:fetchCompletionHandler:(iOS8實測中發現一直調用的是該方法,並未出現調用application didReceiveRemoteNotification的情況),之後會調用application didFinishLaunchingWithOptions:.
對application didFinishLaunchingWithOptions:的launchOptions參數說明如下:
- 若使用者直接啟動,lauchOptions內無資料;
- 若由其他應用程式通過openURL:啟動,則UIApplicationLaunchOptionsURLKey對應的對象為啟動URL(NSURL),UIApplicationLaunchOptionsSourceApplicationKey對應啟動的源應用程式的bundle ID (NSString);
- 若由本地通知啟動,則UIApplicationLaunchOptionsLocalNotificationKey對應的是為啟動應用程式的的本地通知對象(UILocalNotification);
- 若由遠程通知啟動,則UIApplicationLaunchOptionsRemoteNotificationKey對應的是啟動應用程式的的遠程通知資訊userInfo(NSDictionary);
- 其他key還有UIApplicationLaunchOptionsAnnotationKey,UIApplicationLaunchOptionsLocationKey,
UIApplicationLaunchOptionsNewsstandDownloadsKey。
2.APNs推送簡介
每台iOS裝置都會與APNs建立一個TLS串連,並憑此來接受推送訊息.
如果裝置處於斷網離線狀態,APNs會將要推送的訊息緩衝一小段時間,並在裝置上線後推送訊息.只能有一條時間最近的訊息被緩衝,如果在裝置離線期間發送了多條訊息,只會有時間上最後一條訊息被推送.如果裝置長期處於離線狀態,那麼所有緩衝的離線訊息都將被清空.
iOS8之後,推送訊息的最大長度為2Kb,iOS8以前則為256bytes.
此外,還可以設定content-available欄位來發送靜默訊息(使用者介面不會有推送提示,將在之後發文介紹).
看了apple官方文檔,發送推送的道道還挺多,預計將會是一系列的文章+樣本進行說明.由於之後工作中會涉及到後台定位,所有下篇將會是後台定位相關的介紹.
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
iOS推送小結