版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2017.05.14 |
前言
前一篇已經(jīng)對(duì)極光的集成和調(diào)試進(jìn)行了介紹,這一篇?jiǎng)t對(duì)ios的新特新進(jìn)行說明吩翻。
1. 極光推送集成(一)
2. 極光推送集成(二)
3. 極光推送集成(三)
4. 極光推送集成(四)
下面說一下ios新特性的匯總。
ios新特性匯總
一灯节、iOS 10 New Features
1.說明
iOS 10 新增了大量關(guān)于通知的新特性恼琼,詳情參照極光官方的Blog孔祸。
請(qǐng)?zhí)D(zhuǎn)至:iOS 10 新特性,這些新特性都是細(xì)節(jié)的變化驯耻,但是值得大家看看亲族。
二炒考、iOS 10 Service Extension
1.說明
iOS 10新增了Service Extension,官方給出的說明圖如下:
這意味著在APNs到達(dá)我們的設(shè)備之前霎迫,還會(huì)經(jīng)過一層允許用戶自主設(shè)置的Extension服務(wù)進(jìn)行處理斋枢,為APNs增加了多樣性。
2.使用方法
Service Extension使用起來很容易上手知给,首先我們需要?jiǎng)?chuàng)建一個(gè)Service Extension服務(wù)瓤帚,如下圖:
然后這里需要注意幾個(gè)點(diǎn)
- Service Extension的Bundle Identifier不能和Main Target(也就是你自己的App Target)的Bundle Identifier相同,否則會(huì)報(bào)BundeID重復(fù)的錯(cuò)誤涩赢。
- Service Extension的Bundle Identifier需要在Main Target的命名空間下缘滥,比如說Main Target的BundleID為io.jpush.xxx,那么Service Extension的BundleID應(yīng)該類似與io.jpush.xxx.yyy這樣的格式谒主。如果不這么做朝扼,你可能會(huì)遇到一個(gè)錯(cuò)誤。
那么現(xiàn)在你的Service Extension服務(wù)已經(jīng)創(chuàng)建成功了霎肯,此時(shí)你已經(jīng)成功的使用了Service Extension擎颖,但是好像我們還沒有對(duì)它做什么操作,看看你的項(xiàng)目观游,你得到了一個(gè)類搂捧,這個(gè)類中包含兩個(gè)方法。
- didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent *contentToDeliver))contentHandler
- serviceExtensionTimeWillExpire
??我們來看一下第一個(gè)方法的官方解釋:Call contentHandler with the modified notification content to deliver. If the handler is not called before the service's time expires then the unmodified notification will be delivered懂缕。 簡(jiǎn)單解釋一下允跑,APNs到來的時(shí)候會(huì)調(diào)用這個(gè)方法,此時(shí)你可以對(duì)推送過來的內(nèi)容進(jìn)行處理搪柑,然后使用contentHandler完成這次處理聋丝。但是如果時(shí)間太長(zhǎng)了,APNs就會(huì)原樣顯示出來工碾。 也就是說弱睦,我們可以在這個(gè)方法中處理我們的通知,個(gè)性化展示給用戶渊额。 而第二個(gè)方法况木,是對(duì)第一個(gè)方法的補(bǔ)救。第二個(gè)方法會(huì)在過期之前進(jìn)行回調(diào)旬迹,此時(shí)你可以對(duì)你的APNs消息進(jìn)行一下緊急處理火惊。
三、iOS 9集成
1. iOS 9變動(dòng)影響SDK部分
- 增加了bitCode編碼格式,當(dāng)SDK不支持bitCode時(shí)奔垦,用戶集成時(shí)無法開啟bitCode選項(xiàng).
- 現(xiàn)象:用戶集成SDK后無法編譯通過屹耐,錯(cuò)誤日志里包含了bitCode的相關(guān)錯(cuò)誤信息
- 默認(rèn)使用https連接,如果請(qǐng)求為http,需要手動(dòng)配置plist來支持http服務(wù),當(dāng)前我們的服務(wù)器請(qǐng)求都走h(yuǎn)ttp服務(wù)宴倍。
- 現(xiàn)象:用戶集成SDK后张症,所有JPush相關(guān)的http服務(wù)都提示連接錯(cuò)誤或者連接超時(shí),可能是此問題
2. bitCode解決方式
- JPush iOS SDK v1.8.7 及以上版本的SDK,已經(jīng)增加對(duì) iOS 9 新特性 bitCode 的支持.JMessage iOS SDK v2.0.0 及以上版本支持bitCode仓技。
3. Https解決方式
JPush 2.1.9及以上的版本則不需要配置此步驟
- 需要用戶主動(dòng)在當(dāng)前項(xiàng)目的Info.plist中添加NSAppTransportSecurity類型Dictionary。
- 在NSAppTransportSecurity下添加NSAllowsArbitraryLoads類型Boolean俗他,值設(shè)為YES脖捻。
四、iOS 9 UIUserNotificationActionBehaviorTextInput
1. 支持版本
v1.8.0 版本開始
??本次iOS 9在推送方面最大的變化就是修改了推送Category的類型兆衅,在原本的推送categories的基礎(chǔ)上地沮,增加了一個(gè)text Action類型,這個(gè)參數(shù)的目的是用來注冊(cè)通過通知快捷文字輸入的事項(xiàng)羡亩。
??這個(gè)categories由一系列的 UIUserNotificationCategory組成摩疑。每個(gè)UIUserNotificationCategory對(duì)象允許添加一組UIMutableUserNotificationAction類型的參數(shù)來增加通知欄上的項(xiàng)目。如今iOS9在原有的UIMutableUserNotificationAction類型增加了Text輸入類型(UIUserNotificationActionBehaviorTextInput),通過behavior來設(shè)置(只有iOS9才擁有的屬性)畏铆。
回調(diào)的方法iOS9使用了兩個(gè)新的回調(diào)方法來處理點(diǎn)擊按鈕的事件:
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullableNSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(9_0)
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullableNSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(9_0)
說明:
- 當(dāng)Action為UIUserNotificationActionBehaviorTextInput時(shí),需要通過responseInfo的UIUserNotificationActionResponseTypedTextKey來獲取輸入的文字內(nèi)容雷袋,UIUserNotificationTextInputActionButtonTitleKey獲取點(diǎn)擊的按鈕類型。
- 當(dāng)Action為UIUserNotificationActionBehaviorDefault時(shí)辞居,responseInfo為nil楷怒,通過identifier來區(qū)分點(diǎn)擊按鈕分別是什么來做處理。
2. 客戶端設(shè)置
設(shè)置帶有快速回復(fù)內(nèi)容的通知
#ifdef __IPHONE_9_0
UIMutableUserNotificationAction *replyAction = [[UIMutableUserNotificationAction alloc]init];
replyAction.title = @"Reply";
replyAction.identifier = @"comment-reply";
replyAction.activationMode = UIUserNotificationActivationModeBackground;
replyAction.behavior = UIUserNotificationActionBehaviorTextInput;
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];
category.identifier = @"reply";
[category setActions:@[replyAction] forContext:UIUserNotificationActionContextDefault];
#endif
使用回調(diào)函數(shù)
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(9_0)
{
if ([identifier isEqualToString:@"comment-reply"]) {
NSString *response = responseInfo[UIUserNotificationActionResponseTypedTextKey];
//對(duì)輸入的文字作處理
}
completionHandler();
}
3. 服務(wù)端設(shè)置
服務(wù)端payload格式:aps增加category字段瓦灶,當(dāng)該字段與客戶端UIMutableUserNotificationCategory的identifier匹配時(shí)鸠删,觸發(fā)設(shè)定的action和button顯示。
payload example:
{"aps":{"alert":"example", "sound":"default", "badge": 1, "category":"reply"}}
五贼陶、iOS 8 UILocalNotification
本次iOS 8 UILocalNotification增加了三個(gè)參數(shù): region刃泡、regionTriggersOnce、category碉怔。
- region: 用于控制當(dāng)用戶進(jìn)入或者離開某一個(gè)地理位置時(shí)候烘贴,觸發(fā)通知。使用此功能眨层,用戶需要擁有CoreLocation的"when-in-use"權(quán)限庙楚。
- regionTriggersOnce(BOOL):當(dāng)為YES時(shí),通知只會(huì)觸發(fā)一次趴樱,當(dāng)為NO時(shí),通知將會(huì)在每一次進(jìn)入或者離開時(shí)都觸發(fā)酪捡。
- category:如果localNotification通過+[UIUserNotificationSettings settingsForUserNotificationTypes:userNotificationActionSettings:]注冊(cè)了叁征,通過該category可以獲取該通知的注冊(cè)category。
客戶端設(shè)置
使用UILocalNotification
// set localNotification
CLLocationCoordinate2D coordinate2D;
coordinate2D.latitude = 100.0;
coordinate2D.longitude = 100.0;
CLRegion *currentRegion =
[[CLCircularRegion alloc] initWithCenter:coordinate2D
radius:CLLocationDistanceMax
identifier:@"test"];
[APService setLocalNotification:[NSDate dateWithTimeIntervalSinceNow:120]
alertBody:@"test ios8 notification"
badge:0
alertAction:@"取消"
identifierKey:@"1"
userInfo:nil
soundName:nil
region:currentRegion
regionTriggersOnce:YES
category:@"test"];
六逛薇、iOS 8 UIUserNotificationSettings
1. 支持版本
v1.8.0 版本開始捺疼。
- 本次iOS 8在推送方面最大的變化就是修改了推送的注冊(cè)接口,在原本的推送type的基礎(chǔ)上永罚,增加了一個(gè)categories參數(shù)啤呼,這個(gè)參數(shù)的目的是用來注冊(cè)一組和通知關(guān)聯(lián)起來的button的事件卧秘。
- 這個(gè)categories由一系列的 UIUserNotificationCategory組成。每個(gè)UIUserNotificationCategory對(duì)象包含你的app用來響應(yīng)本地或者遠(yuǎn)程通知的信息官扣。每一個(gè)對(duì)象的title作為通知上每一個(gè)button的title展示給用戶翅敌。當(dāng)用戶點(diǎn)擊了某一個(gè)button,系統(tǒng)將會(huì)調(diào)用應(yīng)用內(nèi)的回調(diào)函數(shù)application:handleActionWithIdentifier:forRemoteNotification:completionHandler:或者application:handleActionWithIdentifier:forLocalNotification:completionHandler:惕蹄。
2. 客戶端設(shè)置
使用UIUserNotificationCategory
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
{
NSMutableSet *categories = [NSMutableSet set];
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc] init];
category.identifier = @"identifier";
UIMutableUserNotificationAction *action = [[UIMutableUserNotificationAction alloc] init];
action.identifier = @"test2";
action.title = @"test";
action.activationMode = UIUserNotificationActivationModeBackground;
action.authenticationRequired = YES;
//YES顯示為紅色蚯涮,NO顯示為藍(lán)色
action.destructive = NO;
NSArray *actions = @[ action ];
[category setActions:actions forContext:UIUserNotificationActionContextMinimal];
[categories addObject:category];
}
使用UIUserNotificationType
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
{
[APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:categories];
}
else{
[APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
}
```
使用回調(diào)函數(shù)
```
// Called when your app has been activated by the user selecting an action from
// a remote notification.
// A nil action identifier indicates the default action.
// You should call the completion handler as soon as you've finished handling
// the action.
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo
completionHandler:(void (^)())completionHandler
{
}
```
**3. 服務(wù)端設(shè)置**
服務(wù)端payload格式:aps增加category字段,當(dāng)該字段與客戶端UIMutableUserNotificationCategory的identifier匹配時(shí)卖陵,觸發(fā)設(shè)定的action和button顯示遭顶。
```
payload example:
{"aps":{"alert":"example", "sound":"default", "badge": 1, "category":"identifier"}}
```
---------
## 七、iOS 7 Background Remote Notification
??本次iOS 7在推送方面最大的變化就是允許泪蔫,應(yīng)用收到通知后在后臺(tái)(background)狀態(tài)下運(yùn)行一段代碼棒旗,可用于從服務(wù)器獲取內(nèi)容更新绞灼。功能使用場(chǎng)景:(多媒體)聊天库北,Email更新,基于通知的訂閱內(nèi)容同步等功能闪彼,提升了終端用戶的體驗(yàn)婿滓。Remote Notifications 與之前版本的對(duì)比可以參考下面兩張 Apple 官方的圖片便可一目了然老速。


??如果只攜帶content-available: 1 不攜帶任何badge,sound 和消息內(nèi)容等參數(shù)凸主,則可以不打擾用戶的情況下進(jìn)行內(nèi)容更新等操作即為“Silent Remote Notifications”橘券。

**1. 客戶端設(shè)置**
開啟Remote notifications
需要在Xcode 中修改應(yīng)用的 Capabilities 開啟Remote notifications,請(qǐng)參考下圖:

修改通知處理函數(shù)
當(dāng)注冊(cè)了Backgroud Modes -> Remote notifications 后卿吐,notification 處理函數(shù)一律切換到下面函數(shù)旁舰,后臺(tái)推送代碼也在此函數(shù)中調(diào)用。
```
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler嗡官;
```
**2. 服務(wù)端推送設(shè)置**
??推送消息攜帶 content-available: 1 是Background 運(yùn)行的必須參數(shù)箭窜,如果不攜帶此字段則與iOS7 之前版本的普通推送一樣。
- 使用Web Portal 推送
在“可選設(shè)置內(nèi)”選擇對(duì)應(yīng)的參數(shù)衍腥。

- 使用 API 推送
只需在[Push API v3](http://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#notification) 的 ios 內(nèi)附加content-available":true 字段即可磺樱。
**3. 限制與注意**
- “Silent Remote Notifications”是在 Apple 的限制下有一定的頻率控制,但具體頻率不詳婆咸。所以并不是所有的 “Silent Remote Notifications” 都能按照預(yù)期到達(dá)客戶端觸發(fā)函數(shù)竹捉。
- “Background”下提供給應(yīng)用的運(yùn)行時(shí)間窗是有限制的,如果需要下載較大的文件請(qǐng)參考 Apple 的 NSURLSession 的介紹尚骄。
- “Background Remote Notification” 的前提是要求客戶端處于Background 或 Suspended 狀態(tài)块差,如果用戶通過 App Switcher 將應(yīng)用從后臺(tái) Kill 掉應(yīng)用將不會(huì)喚醒應(yīng)用處理 background 代碼。
更詳細(xì)的說明資料請(qǐng)查閱 Apple 官方的 iOS 開發(fā)文檔。
# 后記
> 未完憨闰,待續(xù)状蜗,謝謝大家~~~
