註冊推播在 iOS 8 上失效

在 iOS 8 上編譯會出現以下 log :

registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later.

 

在 iOS 8 必須用 registerForRemoteNotifications 和 registerUserNotificationSettings 取代
以下程式碼同時支援 iOS 8 與 iOS 7

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
                                                                             settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
                                                                             categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
        
}else {
        
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}

 

如果要檢查推播有沒有打開,這部份也要修改。

UIRemoteNotificationType types;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {

    types = [[UIApplication sharedApplication] currentUserNotificationSettings].types;

}else {

    types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
}

if (types == UIRemoteNotificationTypeNone) {
    // 推播未打開
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *