iOS10實(shí)現(xiàn)推送功能時(shí)的注意點(diǎn)和問(wèn)題總結(jié)
1、在項(xiàng)目 target 中,打開(kāi)Capabilitie —> Push Notifications,并會(huì)自動(dòng)在項(xiàng)目中生成 .entitlement 文件。(很多同學(xué)升級(jí)后,獲取不到 deviceToken,大概率是由于沒(méi)開(kāi)這個(gè)選項(xiàng))

Capabilitie —> Push Notifications

自動(dòng)生成 .entitlement
2、確保添加了 UserNotifications.framework,并 import到 AppDelegate,記得實(shí)現(xiàn) UNUserNotificationCenterDelegate 。
#import <UserNotifications/UserNotifications.h> @interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate> @end
3、在 didFinishLaunchingWithOptions 方法中,首先實(shí)現(xiàn) UNUserNotificationCenter delegate,并使用 UIUserNotificationSettings 請(qǐng)求權(quán)限。
//注意,關(guān)于 iOS10 系統(tǒng)版本的判斷,可以用下面這個(gè)宏來(lái)判斷。不能再用截取字符的方法。
#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
if(SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(@"10.0")){
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error){
if( !error ){
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
}];
}
return YES;
}
4、最后實(shí)現(xiàn)以下兩個(gè)回調(diào)。
//====================For iOS 10====================
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(@"Userinfo %@",notification.request.content.userInfo);
//功能:可設(shè)置是否在應(yīng)用內(nèi)彈出通知
completionHandler(UNNotificationPresentationOptionAlert);
}
//點(diǎn)擊推送消息后回調(diào)
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"Userinfo %@",response.notification.request.content.userInfo);
}
注意:需要根據(jù)系統(tǒng)版本號(hào)來(lái)判斷是否使用新的 UserNotifications.framework,因此,不要著急刪除 iOS 10 以前的代碼。
總結(jié)
以上就是關(guān)于iOS10添加推送功能時(shí)的注意點(diǎn)和問(wèn)題總結(jié),希望這篇文章對(duì)大家開(kāi)發(fā)iOS推送功能能有所幫助,如果有疑問(wèn)大家可以留言交流。
相關(guān)文章
解析iOS開(kāi)發(fā)中的FirstResponder第一響應(yīng)對(duì)象
這篇文章主要介紹了解析iOS開(kāi)發(fā)中的FirstResponder第一響應(yīng)對(duì)象,包括View的FirstResponder的釋放問(wèn)題,需要的朋友可以參考下2015-10-10
iOS 標(biāo)簽Tag列表的實(shí)現(xiàn)代碼
這篇文章主要介紹了本篇文章主要介紹了iOS 標(biāo)簽Tag列表的實(shí)現(xiàn)代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-04-04
iOS中tableView cell分割線的一些設(shè)置技巧
在項(xiàng)目開(kāi)發(fā)中我們會(huì)常常遇到tableView 的cell分割線顯示不全,左邊會(huì)空出一截像素,更有甚者想改變系統(tǒng)的分割線,下面通過(guò)這篇文章來(lái)一起學(xué)習(xí)學(xué)習(xí)在iOS中tableView cell分割線的一些設(shè)置技巧,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-05-05
IOS Xib控件拖拽與頁(yè)面跳轉(zhuǎn)實(shí)例
下面小編就為大家分享一篇IOS Xib控件拖拽與頁(yè)面跳轉(zhuǎn)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Objective-C實(shí)現(xiàn)無(wú)限循環(huán)輪播器
這篇文章主要介紹了Objective-C實(shí)現(xiàn)無(wú)限循環(huán)輪播器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
iOS開(kāi)發(fā)之隨機(jī)生成兩圓之間的標(biāo)準(zhǔn)圓
這篇文章主要給大家介紹了iOS如何實(shí)現(xiàn)在兩圓之間隨機(jī)生成標(biāo)準(zhǔn)圓的方法,實(shí)現(xiàn)的效果類(lèi)似尋找附近人或者附近商家的動(dòng)態(tài)效果,有需要的朋友可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-01-01
iOS實(shí)現(xiàn)屏幕亮度和閃光燈控制的實(shí)例代碼
本篇文章主要介紹了iOS實(shí)現(xiàn)屏幕亮度和閃光燈控制的實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06

