iOS10推送適配完整說明

一年一度的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];

}

完成

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容