1、在項目 target 中,翻開Capabilitie —> Push Notifications,並會主動在項目中生成 .entitlement 文件。(許多同窗進級後,獲得不到 deviceToken,年夜幾率是因為沒開這個選項)

Capabilitie —> Push Notifications

主動生成 .entitlement
2、確保添加了 UserNotifications.framework,並 import到 AppDelegate,記得完成 UNUserNotificationCenterDelegate 。
#import <UserNotifications/UserNotifications.h> @interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate> @end
3、在 didFinishLaunchingWithOptions 辦法中,起首完成 UNUserNotificationCenter delegate,並應用 UIUserNotificationSettings 要求權限。
//留意,關於 IOS10 體系版本的斷定,可以用上面這個宏來斷定。不克不及再用截取字符的辦法。
#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、最初完成以下兩個回調。
//====================For IOS 10====================
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
NSLog(@"Userinfo %@",notification.request.content.userInfo);
//功效:可設置能否在運用內彈出告訴
completionHandler(UNNotificationPresentationOptionAlert);
}
//點擊推送新聞後回調
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{
NSLog(@"Userinfo %@",response.notification.request.content.userInfo);
}
留意:須要依據體系版本號來斷定能否應用新的 UserNotifications.framework,是以,不要焦急刪除 IOS 10 之前的代碼。
總結
以上就是關於iOS10添加推送功效時的留意點和成績總結,願望這篇文章對年夜家開辟iOS推送功效能有所贊助,假如有疑問年夜家可以留言交換。
【iOS10完成推送功效時的留意點和成績總結】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!