IOS 9 не запрашивает разрешение на удаленные уведомления

У меня проблема при попытке запросить разрешение на удаленные уведомления.

Он безупречно работает на iOS 10, но когда я пытаюсь сделать это на устройстве iOS 9, он не показывает никаких предупреждений, а метод делегата UIApplication «application:didRegisterForRemoteNotificationsWithDeviceToken:» не вызывается. Ни "неудачный" метод.

Я тестирую только на реальных устройствах, а не на симуляторе. Код, который я сейчас использую для запроса разрешения, следующий:

-(void)requestPushPermissions {
NSLog(@"Starting register for remote Notification");
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10.0) {
    [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:UNAuthorizationOptionAlert | UNAuthorizationOptionBadge | UNAuthorizationOptionSound completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            NSLog(@"Got a yes!");
        }
        else {
            NSLog(@"Got a no...");
        }
    }];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
else {
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
}
}

Кто-нибудь понял?


person Pointblaster    schedule 10.03.2017    source источник
comment
вы работаете на устройстве или симуляторе? помните, что уведомления плохо работают на симуляторе   -  person Ricardo Alves    schedule 10.03.2017
comment
@RicardoAlves Как я уже писал в вопросе; Я тестирую только на реальных устройствах, а не на симуляторе. Спасибо, в любом случае.   -  person Pointblaster    schedule 10.03.2017


Ответы (1)


попробуйте этот код

#import "AppDelegate.h"

#define SYSTEM_VERSION_GRATERTHAN_OR_EQUALTO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [self registerForRemoteNotification];

    return YES;
}
    - (void)registerForRemoteNotification {
        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];
                }
            }];
        }
        else {
            [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
            [[UIApplication sharedApplication] registerForRemoteNotifications];
        }
    }

включить push-уведомления в возможностях

person Jigar    schedule 10.03.2017
comment
center requestAuthorizationWithOptions: блок может быть вызван не в основном потоке if( !error ){dispatch_async(dispatch_get_main_queue(), ^{ [app registerForRemoteNotifications]; });} - person Roman Solodyashkin; 14.07.2018