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實(shí)現(xiàn)手機(jī)獲取驗(yàn)證碼倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)手機(jī)獲取驗(yàn)證碼倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07iOS中創(chuàng)建Model的最佳實(shí)踐記錄
這篇文章主要給大家介紹了關(guān)于iOS中創(chuàng)建Model的最佳實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用iOS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10iOS開(kāi)發(fā)一個(gè)好看的ActionSheet
本篇文章通過(guò)代碼分享和圖文形式教給大家用IOS寫(xiě)一個(gè)好看的ActionSheet過(guò)程以及注意事項(xiàng),需要的朋友參考下吧。2018-01-01iOS給圖片添加濾鏡&使用openGLES動(dòng)態(tài)渲染圖片詳解及實(shí)例
這篇文章主要介紹了iOS給圖片添加濾鏡&使用openGLES動(dòng)態(tài)渲染圖片詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2016-10-10swift3.0實(shí)現(xiàn)圖片放大縮小動(dòng)畫(huà)效果
這篇文章主要為大家詳細(xì)介紹了swift3.0實(shí)現(xiàn)圖片放大縮小動(dòng)畫(huà)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09iOS中使用URL Scheme進(jìn)行App跳轉(zhuǎn)的教程
這篇文章主要介紹了iOS中使用URL Scheme進(jìn)行App跳轉(zhuǎn)的教程,比如在應(yīng)用內(nèi)提示安裝另一個(gè)應(yīng)用時(shí)就以u(píng)rl打開(kāi)safari然后打開(kāi)app store那樣,需要的朋友可以參考下2016-04-04IOS 開(kāi)發(fā)狀態(tài)欄隱藏的實(shí)現(xiàn)辦法
這篇文章主要介紹了IOS 開(kāi)發(fā)狀態(tài)欄隱藏的實(shí)現(xiàn)辦法的相關(guān)資料,需要的朋友可以參考下2017-02-02