一年一度的iOS大版本更新又開始了,對于不明真相吃瓜群眾來說真是太好啦!對于我們程序員卻意味著disaster...這次的推送架構(gòu)完全推翻以往,所以得從新適配,話不多說,開始吧。
1.在targets的Capabiliies內(nèi)Push Notifications選項(xiàng)開關(guān)打開

Paste_Image.png
然后Background Modes打開如下幾個選項(xiàng)

Paste_Image.png
友情提示上圖幾個選項(xiàng),如果你應(yīng)用內(nèi)沒有需要在后臺音頻播放或者位置更新,第一和第二項(xiàng)還是別勾上了,免得被App Store審核bb...我的剛提交兩天就給我干下來返工了,555
General內(nèi)導(dǎo)入UserNotifications.framework

Paste_Image.png
2.進(jìn)入Appdelegate.m文件
2.1)
# #import <UserNotifications/UserNotifications.h>
遵循UNUserNotificationCenterDelegate協(xié)議
@interface AppDelegate()<UNUserNotificationCenterDelegate>
2.2)
- (BOOL)application:(UIApplication
*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法內(nèi)調(diào)用registRemoteNotifications方法
//20160930 注冊通知APNS
[self registRemoteNotifications];
該方法具體如下
- (void)registRemoteNotifications {
if ([[[UIDevice currentDevice] systemVersion]floatValue]>=10.0) {
//申請用戶同意
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error) {
NSLog(@"succeeded!");
}
if (granted) {
[center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSLog(@"remoteNotificationSetting: %@", settings);
}];
}
}];
}
float ios_version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (ios_version >= 8.0){//iOS8-iOS10
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert];
}
}
2.3)
再實(shí)現(xiàn)如下兩個代理方法
# #pragma mark --ios10推送回調(diào)
//前臺回調(diào)
- (void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification
*)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler
{
[self application:[UIApplication sharedApplication] didReceiveRemoteNotification:notification.request.content.userInfo];
}
//后臺回調(diào)
- (void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse
*)response withCompletionHandler:(void(^)())completionHandler
{
[self application:[UIApplication sharedApplication] didReceiveRemoteNotification:response.notification.request.content.userInfo];
}
完成