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

Capabilitie —> Push Notifications

自動生成 .entitlement
2、確保添加了 UserNotifications.framework,并 import到 AppDelegate,記得實現(xiàn) UNUserNotificationCenterDelegate 。
#import <UserNotifications/UserNotifications.h> @interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate> @end
3、在 didFinishLaunchingWithOptions 方法中,首先實現(xiàn) UNUserNotificationCenter delegate,并使用 UIUserNotificationSettings 請求權(quán)限。
//注意,關(guān)于 iOS10 系統(tǒng)版本的判斷,可以用下面這個宏來判斷。不能再用截取字符的方法。
#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、最后實現(xiàn)以下兩個回調(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ào)
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"Userinfo %@",response.notification.request.content.userInfo);
}
注意:需要根據(jù)系統(tǒng)版本號來判斷是否使用新的 UserNotifications.framework,因此,不要著急刪除 iOS 10 以前的代碼。
總結(jié)
以上就是關(guān)于iOS10添加推送功能時的注意點和問題總結(jié),希望這篇文章對大家開發(fā)iOS推送功能能有所幫助,如果有疑問大家可以留言交流。
相關(guān)文章
解析iOS開發(fā)中的FirstResponder第一響應(yīng)對象
這篇文章主要介紹了解析iOS開發(fā)中的FirstResponder第一響應(yīng)對象,包括View的FirstResponder的釋放問題,需要的朋友可以參考下2015-10-10
iOS中tableView cell分割線的一些設(shè)置技巧
在項目開發(fā)中我們會常常遇到tableView 的cell分割線顯示不全,左邊會空出一截像素,更有甚者想改變系統(tǒng)的分割線,下面通過這篇文章來一起學(xué)習(xí)學(xué)習(xí)在iOS中tableView cell分割線的一些設(shè)置技巧,需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05
Objective-C實現(xiàn)無限循環(huán)輪播器
這篇文章主要介紹了Objective-C實現(xiàn)無限循環(huán)輪播器的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05
iOS開發(fā)之隨機生成兩圓之間的標(biāo)準(zhǔn)圓
這篇文章主要給大家介紹了iOS如何實現(xiàn)在兩圓之間隨機生成標(biāo)準(zhǔn)圓的方法,實現(xiàn)的效果類似尋找附近人或者附近商家的動態(tài)效果,有需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-01-01

