iOS---iOS10適配iOS當前全部系統的遠程推送

1、iOS推送通知簡介html

衆所周知蘋果的推送通知從iOS3開始出現, 每年都會更新一些新的用法. 譬如iOS7出現的Silent remote notifications(遠程靜默推送), iOS8出現的Category(分類, 也可稱之爲快捷回覆), iOS9出現的Text Input action(文本框快捷回覆).xcode

而在iOS10, 蘋果可謂是大刀闊斧般的, 對遠程通知和本地通知進行了大範圍的更新. iOS10推出了全新的UserNotifications框架(iOS10以前從屬於UIKit框架).服務器

新的推送通知框架, 整合了本地推送和遠程推送的點擊處理方法, 使得之前專門處理推送點擊的方法只能處理靜默推送了.app

2、遠程推送通知介紹框架

一、什麼是遠程推送ide

在聯網的狀況下,由遠程服務器推送給客戶端的通知,又稱APNs(Apple Push Notification Services)無論應用是打開仍是關閉的狀況下,都能接收到服務器推送的遠程通知在聯網狀態下,全部蘋果設備都會與蘋果服務器創建長鏈接fetch

二、遠程推送的實現原理:加密

      1.打開App時: 發送UDIDBundleIDAPNs加密後返回deviceToken代理

      2.獲取Token後,App調用接口,將用戶身份信息和deviceToken發給服務器,服務器記錄調試

      3.當推送消息時, 服務器按照用戶身份信息找到存儲的deviceToken,將消息和deviToken發送給APNs

      4.蘋果的APNs經過deviceToken, 找到指定設備的指定程序, 並將消息推送給用戶

三、實現遠程推送功能的前提

      1.真機

      2.調試階段的證書

         iOS_development.cer 用於真機調試的證書

         aps_development.cer 用於真機推送調試能的證書

         xxx.mobileprovision 描述文件,記錄了可以調試的手機、電腦和程序

      3.發佈階段的證書

          iOS_distribution.cer 用於發佈app的證書

          aps.cer 用於發佈時,讓app有推送功能的證書

          xxx.mobileprovision 描述文件,記錄了可以發佈app的電腦

如何配置證書,請參考個人另外一博文:  iOS-推送,證書申請,本地推送

2、 iOS10遠程推送通知的處理方法

當點擊了推送後, 若是你但願進行處理. 那麼在iOS10中, 還須要設置UNUserNotificationCenterdelegate, 並遵照UNUserNotificationCenterDelegate協議.

以及實現下面實現3個方法, 用於處理點擊通知時的不一樣狀況的處理

      willPresentNotification:withCompletionHandler 用於前臺運行

      didReceiveNotificationResponse:withCompletionHandler 用於後臺及程序退出

      didReceiveRemoteNotification:fetchCompletionHandler用於靜默推送

1.前臺運行 會調用的方法

前臺運行: 指的是程序正在運行中, 用戶能看見程序的界面.

iOS10會出現通知橫幅, 而在之前的框架中, 前臺運行時, 不會出現通知的橫幅. 

代碼開始前的設置 

iOS 10 的推送 與原來的都不同,他把本地的推送 跟 遠程的推送結合到一塊兒了,UserNotifications.framework 庫。在使用推送的時候,先開啓通知的開關。

就是上面這個。當你開啓後,xcode 會自動幫你在 項目裏面建立一個文件,xxxx.entitlements.

這個文件是系統幫你建立的,不用管它。 

在appDeletgate 文件裏面須要先導入 UNUserNotificationCenterDelegate 這個代理。他的代理方法分別是

  1. - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler __IOS_AVAILABLE(10.0) __TVOS_AVAILABLE(10.0) __WATCHOS_AVAILABLE(3.0);  
  2.   
  3. - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler __IOS_AVAILABLE(10.0)   

  代碼以下: 

  2
  3 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  4 
  5     
  6 
  7     /**
  8 
  9      註冊遠程推送和本地通知,適配至最新系統,目前是 iOS10
 10 
 11      */
 12 
 13     [self registerRemoteNotificationsForAlliOSSystemVersion];
 14 
 15   
 16 
 17     
 18 
 19     // Override point for customization after application launch.
 20 
 21     return YES;
 22 
 23 }
 24 
 25  
 26 
 27 /**
 28 
 29     註冊遠程推送和本地通知,適配至最新系統,目前是 iOS10
 30 
 31  */
 32 
 33 -(void)registerRemoteNotificationsForAlliOSSystemVersion{
 34 
 35     
 36 
 37     //
 38 
 39     
 40 
 41     
 42 
 43     //導入文件 #import <UserNotifications/UserNotifications.h>
 44 
 45     //去capabilities(功能)設置這邊打開 pushNotifications,而且打開  backgroundModes 中的backgroundFentch,Remote Notifications
 46 
 47     CGFloat version = [[[UIDevice currentDevice] systemVersion] floatValue];
 48 
 49     if (version >= 10.0) {//10.0及其以上
 50 
 51         UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
 52 
 53         //請求通知權限, 本地和遠程共用
 54 
 55         //                                      設定通知可選提示類型
 56 
 57         [center requestAuthorizationWithOptions:UNAuthorizationOptionCarPlay | UNAuthorizationOptionSound | UNAuthorizationOptionBadge | UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
 58 
 59             if (error) {
 60 
 61                 NSLog(@"iOS10請求 接受遠程和本地通知 受權失敗:<%@>",[error description]);
 62 
 63             }
 64 
 65             
 66 
 67             if (granted) {
 68 
 69                 NSLog(@" iOS 10 request notification success");
 70 
 71                 NSLog(@"請求成功");
 72 
 73             }else{
 74 
 75                 NSLog(@" iOS 10 request notification fail");
 76 
 77                 NSLog(@"請求失敗");
 78 
 79             }
 80 
 81         }];
 82 
 83         
 84 
 85         //設置通知的代理
 86 
 87         center.delegate = self;//1.遵照UNUserNotificationCenterDelegate協議,2.成爲代理;3.實現代理回調方法
 88 
 89     }else if (version>=8.0){//8.0--->10.0
 90 
 91         //請求用戶受權                                                                       受權收到推送時有哪些提醒方式能夠選
 92 
 93         // 聲音、角標、彈窗
 94 
 95         UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert categories:nil];
 96 
 97         [[UIApplication sharedApplication] registerUserNotificationSettings:setting];
 98 
 99     }else{//8.0如下
100 
101         UIRemoteNotificationType type =  UIRemoteNotificationTypeSound| UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge;
102 
103         [[UIApplication sharedApplication] registerForRemoteNotificationTypes:type];
104 
105     }
106 
107     
108 
109     //註冊通知
110 
111     [[UIApplication sharedApplication] registerForRemoteNotifications];
112 
113     
114 
115     
116 
117  
118 
119 }
120 
121 #pragma mark-推送通知
122 
123 //註冊成功
124 
125 -(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
126 
127     
128 
129     NSString *token = [deviceToken description]; //獲取
130 
131     
132 
133     token =  [token stringByReplacingOccurrencesOfString:@" " withString:@""];
134 
135     token =  [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
136 
137     token =  [token stringByReplacingOccurrencesOfString:@">" withString:@""];
138 
139     
140 
141     NSLog(@"request notificatoin token success. %@",token);
142 
143     
144 
145  
146 
147  
148 
149 }
150 
151 //註冊失敗
152 
153 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
154 
155 {
156 
157     NSLog(@"request notification Token fail. %@",error.localizedDescription);
158 
159 }
160 
161  
162 
163 #pragma mark  iOS 10 獲取推送信息 UNUserNotificationCenter---Delegate
164 
165  
166 
167 //APP在前臺的時候收到推送的回調
168 
169 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
170 
171 {
172 
173     
174 
175      
176 
177     UNNotificationContent *content =  notification.request.content;
178 
179     NSDictionary *userInfo = content.userInfo;
180 
181     
182 
183     [self handleRemoteNotificationContent:userInfo];
184 
185     
186 
187     //前臺運行推送 顯示紅色Label
188 
189     [self showLabelWithUserInfo:userInfo color:[UIColor redColor]];
190 
191     
192 
193     
194 
195     //能夠設置當收到通知後, 有哪些效果呈現(提醒/聲音/數字角標)
196 
197     //能夠執行設置 彈窗提醒 和 聲音
198 
199     completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge);
200 
201 }
202 
203 //APP在後臺,點擊推送信息,進入APP後執行的回調
204 
205 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
206 
207 {
208 
209    
210 
211     
212 
213     UNNotificationContent *content  = response.notification.request.content;
214 
215     NSDictionary *userInfo = content.userInfo;
216 
217     
218 
219     [self handleRemoteNotificationContent:userInfo];
220 
221     //後臺及退出推送 顯示綠色Label
222 
223     [self showLabelWithUserInfo:userInfo color:[UIColor greenColor]];
224 
225     
226 
227     completionHandler();
228 
229 }
230 
231  
232 
233 - (void)handleRemoteNotificationContent:(NSDictionary *)userInfo
234 
235 {
236 
237     NSLog(@" iOS 10 after Notificatoin message:\n %@",userInfo);
238 
239 }
240 
241 #pragma mark iOS 10 以前 獲取通知的信息
242 
243 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
244 
245     
246 
247     //靜默推送 顯示藍色Label
248 
249     [self showLabelWithUserInfo:userInfo color:[UIColor blueColor]];
250 
251     
252 
253     completionHandler(UIBackgroundFetchResultNewData);
254 
255 }
256 
257 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
258 
259 {
260 
261     NSLog(@"iOS 10 before Notification message。\n  %@",userInfo);
262 
263 }
264 
265  
266 
267  
268 
269 - (void)showLabelWithUserInfo:(NSDictionary *)userInfo color:(UIColor *)color
270 
271 {
272 
273     UILabel *label = [UILabel new];
274 
275     label.backgroundColor = color;
276 
277     label.frame = CGRectMake(0, 250, [UIScreen mainScreen].bounds.size.width, 300);
278 
279     label.text = userInfo.description;
280 
281     label.numberOfLines = 0;
282 
283     [[UIApplication sharedApplication].keyWindow addSubview:label];
284 
285 }
286 
287  
288 
289  
相關文章
相關標籤/搜索