iOS10推送適配完整說明

Auditore發表於2018-01-03

一年一度的iOS大版本更新又開始了,對於不明真相吃瓜群眾來說真是太好啦!對於我們程式設計師卻意味著disaster...這次的推送架構完全推翻以往,所以得從新適配,話不多說,開始吧。 1.在targets的Capabiliies內Push Notifications選項開關開啟

Paste_Image.png

然後Background Modes開啟如下幾個選項

Paste_Image.png

友情提示上圖幾個選項,如果你應用內沒有需要在後臺音訊播放或者位置更新,第一和第二項還是別勾上了,免得被App Store稽核bb...我的剛提交兩天就給我幹下來返工了,555

General內匯入UserNotifications.framework

Paste_Image.png

2.進入Appdelegate.m檔案

2.1) #import <UserNotifications/UserNotifications.h>

遵循UNUserNotificationCenterDelegate協議

@interface AppDelegate()<UNUserNotificationCenterDelegate>

2.2)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

方法內呼叫registRemoteNotifications方法

//20160930 註冊通知APNS

[self registRemoteNotifications];

該方法具體如下

1.- (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) 再實現如下兩個代理方法 #pragma mark --ios10推送回撥 //前臺回撥

1)- (void)userNotificationCenter:(UNUserNotificationCenter*)center willPresentNotification:(UNNotification
*)notification withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler

{

    [self application:[UIApplication sharedApplication] didReceiveRemoteNotification:notification.request.content.userInfo];

}

//後臺回撥

2)   - (void)userNotificationCenter:(UNUserNotificationCenter*)center didReceiveNotificationResponse:(UNNotificationResponse
*)response withCompletionHandler:(void(^)())completionHandler

{

    [self application:[UIApplication sharedApplication] didReceiveRemoteNotification:response.notification.request.content.userInfo];

}
複製程式碼

完成

相關文章