iOS10推送之基礎(chǔ)知識(shí)(必看篇)
前言
在北京時(shí)間9月14號(hào)凌晨1點(diǎn),蘋(píng)果正式推送iOS 10正式版,下面給大家詳細(xì)的介紹iOS10推送的基礎(chǔ)知識(shí),在看完簡(jiǎn)單入門(mén)篇大家就可以簡(jiǎn)單適配了,然后再通過(guò)中級(jí)篇的內(nèi)容,相信對(duì)大家學(xué)習(xí)理解有很大的幫助,下面話(huà)不多說(shuō)了,來(lái)看看吧。
一、簡(jiǎn)單入門(mén)篇
相對(duì)簡(jiǎn)單的推送證書(shū)以及環(huán)境的問(wèn)題,我就不在這里講啦,我在這里說(shuō)的,是指原有工程的適配。
1.首先我們需要打開(kāi)下面的開(kāi)關(guān)。所有的推送平臺(tái),不管是極光還是什么的,要想收到推送,這個(gè)是必須打開(kāi)的喲~
之后,系統(tǒng)會(huì)生成一個(gè)我們以前沒(méi)見(jiàn)過(guò)的文件,如圖:
可能產(chǎn)生的問(wèn)題:之前有朋友反饋過(guò),將開(kāi)發(fā)環(huán)境由 development 變成 production ,在開(kāi)關(guān)這里會(huì)產(chǎn)生錯(cuò)誤,如圖:
如果大家點(diǎn)擊Fix issue之后,會(huì)驚奇的發(fā)現(xiàn),APS Environment由 production 又變成 development 了。
解決辦法:我的建議是不做任何修改。
經(jīng)過(guò)我的測(cè)試,打包之后,生成的ipa包內(nèi),是沒(méi)有這個(gè).entitlements 文件的。經(jīng)過(guò)測(cè)試,我發(fā)現(xiàn)是可以正常收到推送信息的。測(cè)試的方法如下,大家也可以測(cè)試一下。
測(cè)試方法:打包之后安裝ipa文件,然后利用極光推送,選擇生產(chǎn)環(huán)境,推送,即可。
經(jīng)過(guò)上面的操作,你就會(huì)驚奇的發(fā)現(xiàn),推送已經(jīng)適配完畢了,iOS10的系統(tǒng),已經(jīng)可以正常接收通知了。
二、中級(jí)篇
這里我會(huì)給大家講一講iOS10的推送,如何注冊(cè),通過(guò)什么代理,哪些方法可以用,哪些方法不可以用。
1.系統(tǒng)自帶方法
大家不管是使用三方平臺(tái)的推送,還是系統(tǒng)自帶的推送,都先應(yīng)該了解下系統(tǒng)自帶方法,如何實(shí)現(xiàn)遠(yuǎn)程通知的實(shí)現(xiàn)。
第一步導(dǎo)入#import <UserNotifications/UserNotifications.h>
且要遵守<UNUserNotificationCenterDelegate>的協(xié)議,在Appdelegate.m中。
這里需要注意,我們最好寫(xiě)成這種形式
#ifdef NSFoundationVersionNumber_iOS_9_x_Max #import <UserNotifications/UserNotifications.h> #endif
第二步我們需要在 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中注冊(cè)通知,代碼如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { //iOS10特有 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; // 必須寫(xiě)代理,不然無(wú)法監(jiān)聽(tīng)通知的接收與點(diǎn)擊 center.delegate = self; [center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound) completionHandler:^(BOOL granted, NSError * _Nullable error) { if (granted) { // 點(diǎn)擊允許 NSLog(@"注冊(cè)成功"); [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) { NSLog(@"%@", settings); }]; } else { // 點(diǎn)擊不允許 NSLog(@"注冊(cè)失敗"); } }]; }else if ([[UIDevice currentDevice].systemVersion floatValue] >8.0){ //iOS8 - iOS10 [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge categories:nil]]; }else if ([[UIDevice currentDevice].systemVersion floatValue] < 8.0) { //iOS8系統(tǒng)以下 [application registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound]; } // 注冊(cè)獲得device Token [[UIApplication sharedApplication] registerForRemoteNotifications];
其中,獲得Device Token的方法是沒(méi)有改變的。
// 獲得Device Token - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { NSLog(@"%@", [NSString stringWithFormat:@"Device Token: %@", deviceToken]); } // 獲得Device Token失敗 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error); }
此次iOS10系統(tǒng)的更新,蘋(píng)果給了我們2個(gè)代理方法來(lái)處理通知的接收和點(diǎn)擊事件,這兩個(gè)方法在<UNUserNotificationCenterDelegate>的協(xié)議中,大家可以查看下。此外,蘋(píng)果把本地通知跟遠(yuǎn)程通知合二為一。區(qū)分本地通知跟遠(yuǎn)程通知的類(lèi)是UNPushNotificationTrigger.h
類(lèi)中,UNPushNotificationTrigger
的類(lèi)型是新增加的,通過(guò)它,我們可以得到一些通知的觸發(fā)條件,在使用時(shí),我們不應(yīng)該直接使用這個(gè)類(lèi),應(yīng)當(dāng)使用它的子類(lèi)。
我簡(jiǎn)單點(diǎn)說(shuō)
1.UNPushNotificationTrigger
(遠(yuǎn)程通知) 遠(yuǎn)程推送的通知類(lèi)型
2.UNTimeIntervalNotificationTrigger
(本地通知) 一定時(shí)間之后,重復(fù)或者不重復(fù)推送通知。我們可以設(shè)置timeInterval
(時(shí)間間隔)和repeats
(是否重復(fù))。
3.UNCalendarNotificationTrigger
(本地通知) 一定日期之后,重復(fù)或者不重復(fù)推送通知 例如,你每天8點(diǎn)推送一個(gè)通知,只要dateComponents
為8,如果你想每天8點(diǎn)都推送這個(gè)通知,只要repeats為YES就可以了。
4.UNLocationNotificationTrigger
(本地通知)地理位置的一種通知,
當(dāng)用戶(hù)進(jìn)入或離開(kāi)一個(gè)地理區(qū)域來(lái)通知。在CLRegion標(biāo)識(shí)符必須是唯一的。因?yàn)槿绻嗤臉?biāo)識(shí)符來(lái)標(biāo)識(shí)不同區(qū)域的UNNotificationRequests
,會(huì)導(dǎo)致不確定的行為。
接收通知的代碼如下:
// iOS 10收到通知 - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ NSDictionary * userInfo = notification.request.content.userInfo; UNNotificationRequest *request = notification.request; // 收到推送的請(qǐng)求 UNNotificationContent *content = request.content; // 收到推送的消息內(nèi)容 NSNumber *badge = content.badge; // 推送消息的角標(biāo) NSString *body = content.body; // 推送消息體 UNNotificationSound *sound = content.sound; // 推送消息的聲音 NSString *subtitle = content.subtitle; // 推送消息的副標(biāo)題 NSString *title = content.title; // 推送消息的標(biāo)題 if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { NSLog(@"iOS10 前臺(tái)收到遠(yuǎn)程通知:%@", [self logDic:userInfo]); } else { // 判斷為本地通知 NSLog(@"iOS10 前臺(tái)收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo); } completionHandler(UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionSound|UNNotificationPresentationOptionAlert); // 需要執(zhí)行這個(gè)方法,選擇是否提醒用戶(hù),有Badge、Sound、Alert三種類(lèi)型可以設(shè)置 }
下面的代碼則是通知的點(diǎn)擊事件:
// 通知的點(diǎn)擊事件 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{ NSDictionary * userInfo = response.notification.request.content.userInfo; UNNotificationRequest *request = response.notification.request; // 收到推送的請(qǐng)求 UNNotificationContent *content = request.content; // 收到推送的消息內(nèi)容 NSNumber *badge = content.badge; // 推送消息的角標(biāo) NSString *body = content.body; // 推送消息體 UNNotificationSound *sound = content.sound; // 推送消息的聲音 NSString *subtitle = content.subtitle; // 推送消息的副標(biāo)題 NSString *title = content.title; // 推送消息的標(biāo)題 if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { NSLog(@"iOS10 收到遠(yuǎn)程通知:%@", [self logDic:userInfo]); } else { // 判斷為本地通知 NSLog(@"iOS10 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo); } // Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called. completionHandler(); // 系統(tǒng)要求執(zhí)行這個(gè)方法 }
在點(diǎn)擊事件中,如果我們不寫(xiě)completionHandler()
這個(gè)方法,可能會(huì)報(bào)一下的錯(cuò)誤,希望大家注意下~
Warning: UNUserNotificationCenter delegate received call to -userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler: but the completion handler was never called.
最后最后,我們要大家補(bǔ)充一下,舊版本的一些方法,方便大家擴(kuò)充iOS10的通知的通知,不影響原有邏輯。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSLog(@"iOS6及以下系統(tǒng),收到通知:%@", [self logDic:userInfo]); } - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler: (void (^)(UIBackgroundFetchResult))completionHandler { NSLog(@"iOS7及以上系統(tǒng),收到通知:%@", [self logDic:userInfo]); completionHandler(UIBackgroundFetchResultNewData); }
2.極光推送(需要下載最新的版本)
如果用到三方的一些平臺(tái),做推送就會(huì)更為簡(jiǎn)單。
1.注冊(cè)通知的代碼如下
if ([[UIDevice currentDevice].systemVersion floatValue] >= 10.0) { #ifdef NSFoundationVersionNumber_iOS_9_x_Max JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init]; entity.types = UNAuthorizationOptionAlert|UNAuthorizationOptionBadge|UNAuthorizationOptionSound; [JPUSHService registerForRemoteNotificationConfig:entity delegate:self]; #endif } else if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) { //可以添加自定義categories [JPUSHService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; } else { //categories 必須為nil [JPUSHService registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert) categories:nil]; }
注冊(cè)完成之后,我們則需要加入極光推送更新后,新加入的2個(gè)方法,這兩個(gè)方法在<JPUSHRegisterDelegate>
代理方法中。
/* * @brief handle UserNotifications.framework [willPresentNotification:withCompletionHandler:] * @param center [UNUserNotificationCenter currentNotificationCenter] 新特性用戶(hù)通知中心 * @param notification 前臺(tái)得到的的通知對(duì)象 * @param completionHandler 該callback中的options 請(qǐng)使用UNNotificationPresentationOptions */ - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger options))completionHandler; /* * @brief handle UserNotifications.framework [didReceiveNotificationResponse:withCompletionHandler:] * @param center [UNUserNotificationCenter currentNotificationCenter] 新特性用戶(hù)通知中心 * @param response 通知響應(yīng)對(duì)象 * @param completionHandler */ - (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler;
使用時(shí),只需要在上面的代碼中添加極光的處理方法就可以了,具體使用如下圖:
if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) { // 這個(gè)方法,不管是收到通知代理還是點(diǎn)擊通知的代理,如果使用極光推送,我們都是需要增加這個(gè)方法的。 [JPUSHService handleRemoteNotification:userInfo]; NSLog(@"iOS10 收到遠(yuǎn)程通知:%@", [self logDic:userInfo]); [rootViewController addNotificationCount]; } else { // 判斷為本地通知 NSLog(@"iOS10 收到本地通知:{\\\\nbody:%@,\\\\ntitle:%@,\\\\nsubtitle:%@,\\\\nbadge:%@,\\\\nsound:%@,\\\\nuserInfo:%@\\\\n}",body,title,subtitle,badge,sound,userInfo); }
通過(guò)上面的文章,相信大家已經(jīng)可以初步了解新版本的推送,要如何處理啦~
總結(jié)
以上就是iOS10推送之基礎(chǔ)知識(shí)的全部?jī)?nèi)容,不知道大家都學(xué)會(huì)了嗎?希望這篇文章能對(duì)各位iOS開(kāi)發(fā)者們有所幫助,如果有疑問(wèn)大家可以留言交流。
- iOS10 適配遠(yuǎn)程推送功能實(shí)現(xiàn)代碼
- 更新了Xcode8 及 iOS10遇到的問(wèn)題小結(jié)
- iOS10全新推送功能實(shí)現(xiàn)代碼
- Xcode8以及iOS10適配等常見(jiàn)問(wèn)題匯總(整理篇)
- iOS10開(kāi)發(fā)和Xcode 8新特性及常見(jiàn)問(wèn)題解析
- IOS10 解決權(quán)限崩潰問(wèn)題詳解
- iOS10適配之權(quán)限Crash問(wèn)題的完美解決方案
- 110.iOS10新特性適配教程XCode8新特性解析
- 解析iOS10中的極光推送消息的適配
- Xcode8、iOS10升級(jí)問(wèn)題記錄
- iOS10 推送最新特性研究
- IOS10 遠(yuǎn)程推送適配詳細(xì)介紹
相關(guān)文章
ios實(shí)現(xiàn)app強(qiáng)制更新功能
本篇文章主要介紹了ios實(shí)現(xiàn)app強(qiáng)制更新功能,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05iOS應(yīng)用中使用Auto Layout實(shí)現(xiàn)自定義cell及拖動(dòng)回彈
這篇文章主要介紹了iOS應(yīng)用中使用Auto Layout實(shí)現(xiàn)自定義cell及拖動(dòng)回彈的方法,自定義UITableViewCell并使用Auto Layout對(duì)其進(jìn)行約束可以方便地針對(duì)多尺寸屏幕進(jìn)行調(diào)整,代碼為Swift語(yǔ)言,需要的朋友可以參考下2016-03-03iOS開(kāi)發(fā)中UIImageView控件的常用操作整理
這篇文章主要介紹了iOS開(kāi)發(fā)中UIImageView控件的常用操作整理,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01IOS 開(kāi)發(fā)之自定義按鈕實(shí)現(xiàn)文字圖片位置隨意定制
這篇文章主要介紹了IOS 開(kāi)發(fā)之自定義按鈕實(shí)現(xiàn)文字圖片位置隨意定制的相關(guān)資料,這里附有實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-12-12IOS 避免self循環(huán)引用的方法的實(shí)例詳解
這篇文章主要介紹了IOS 避免self循環(huán)引用的方法的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09MAUI模仿iOS多任務(wù)切換卡片滑動(dòng)的交互實(shí)現(xiàn)代碼
這篇文章主要介紹了[MAUI]模仿iOS多任務(wù)切換卡片滑動(dòng)的交互實(shí)現(xiàn),使用.NET MAU實(shí)現(xiàn)跨平臺(tái)支持,本項(xiàng)目可運(yùn)行于Android、iOS平臺(tái),需要的朋友可以參考下2023-05-05iOS開(kāi)發(fā)中runtime常用的幾種方法示例總結(jié)
Runtime也就是所謂的“運(yùn)行時(shí)”,因?yàn)槭窃谶\(yùn)行時(shí)實(shí)現(xiàn)的。下面這篇文章主要給大家介紹了關(guān)于iOS開(kāi)發(fā)中runtime常用的幾種方法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-08-08