極光推送集成(五)

版本記錄

版本號(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,官方給出的說明圖如下:

ios新特性-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ù)瓤帚,如下圖:

創(chuàng)建Service Extension

然后這里需要注意幾個(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 官方的圖片便可一目了然老速。

![ios6.0](http://upload-images.jianshu.io/upload_images/3691932-961a22d41e74cca0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/620)


![ios7.0](http://upload-images.jianshu.io/upload_images/3691932-5e29822c846eeb60.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/620)

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

![Silent Remote Notifications](http://upload-images.jianshu.io/upload_images/3691932-ed375eb813e8f56c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/620)

**1. 客戶端設(shè)置**

開啟Remote notifications

需要在Xcode 中修改應(yīng)用的 Capabilities 開啟Remote notifications,請(qǐng)參考下圖:

![打開通知權(quán)限](http://upload-images.jianshu.io/upload_images/3691932-1f8cbd1faad51423.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/620)

修改通知處理函數(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ù)衍腥。

![web 推送](http://upload-images.jianshu.io/upload_images/3691932-27081e9cdb77673b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/620)
- 使用 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ù)状蜗,謝謝大家~~~

![大與小](http://upload-images.jianshu.io/upload_images/3691932-3da5c00b2a72ee13.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/620)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市鹉动,隨后出現(xiàn)的幾起案子轧坎,更是在濱河造成了極大的恐慌,老刑警劉巖训裆,帶你破解...
    沈念sama閱讀 221,635評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件眶根,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡边琉,警方通過查閱死者的電腦和手機(jī)属百,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來变姨,“玉大人族扰,你說我怎么就攤上這事《ㄅ罚” “怎么了渔呵?”我有些...
    開封第一講書人閱讀 168,083評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)砍鸠。 經(jīng)常有香客問我扩氢,道長(zhǎng),這世上最難降的妖魔是什么爷辱? 我笑而不...
    開封第一講書人閱讀 59,640評(píng)論 1 296
  • 正文 為了忘掉前任录豺,我火速辦了婚禮,結(jié)果婚禮上饭弓,老公的妹妹穿的比我還像新娘双饥。我一直安慰自己,他們只是感情好弟断,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評(píng)論 6 397
  • 文/花漫 我一把揭開白布咏花。 她就那樣靜靜地躺著,像睡著了一般阀趴。 火紅的嫁衣襯著肌膚如雪昏翰。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,262評(píng)論 1 308
  • 那天刘急,我揣著相機(jī)與錄音矩父,去河邊找鬼。 笑死排霉,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播攻柠,決...
    沈念sama閱讀 40,833評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼球订,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了瑰钮?” 一聲冷哼從身側(cè)響起冒滩,我...
    開封第一講書人閱讀 39,736評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎浪谴,沒想到半個(gè)月后开睡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,280評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡苟耻,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評(píng)論 3 340
  • 正文 我和宋清朗相戀三年篇恒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片凶杖。...
    茶點(diǎn)故事閱讀 40,503評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡胁艰,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出智蝠,到底是詐尸還是另有隱情腾么,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布杈湾,位于F島的核電站解虱,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏漆撞。R本人自食惡果不足惜殴泰,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望叫挟。 院中可真熱鬧艰匙,春花似錦、人聲如沸抹恳。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,340評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)奋献。三九已至健霹,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間瓶蚂,已是汗流浹背糖埋。 一陣腳步聲響...
    開封第一講書人閱讀 33,460評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留窃这,地道東北人瞳别。 一個(gè)月前我還...
    沈念sama閱讀 48,909評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親祟敛。 傳聞我的和親對(duì)象是個(gè)殘疾皇子疤坝,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容