Подтвердить что ты не робот

RegisterForRemoteNotificationTypes: не поддерживается в iOS 8.0 и более поздних версиях

При попытке зарегистрироваться для push-уведомлений в iOS 8.x:

application.registerForRemoteNotificationTypes(UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound)

Я получаю следующую ошибку:

registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later.

Любые идеи, что это новый способ сделать это? Он работает, когда я запускаю это приложение Swift на iOS 7.x.

ИЗМЕНИТЬ

В iOS 7.x, когда я включаю условный код, который я получаю (либо SystemVersion условный, либо #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000)

dyld: Symbol not found: _OBJC_CLASS_$_UIUserNotificationSettings
4b9b3361

Ответ 1

Как вы описали, вам нужно будет использовать другой метод, основанный на разных версиях iOS. Если ваша команда использует как Xcode 5 (который не знает о каких-либо селекторах iOS 8), так и Xcode 6 (в настоящее время в бета-версии), вам необходимо будет использовать условную компиляцию следующим образом:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    // use registerUserNotificationSettings
} else {
    // use registerForRemoteNotificationTypes:
}
#else
// use registerForRemoteNotificationTypes:
#endif

Если вы используете только Xcode 6 (beta), вы можете использовать только это:

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
    // use registerUserNotificationSettings
} else {
    // use registerForRemoteNotificationTypes:
}

Причина заключается в том, что способ получения разрешений для уведомлений изменился в iOS 8. A UserNotification - это сообщение, отображаемое пользователю, будь то удаленное или локальное. Вам нужно получить разрешение на показ. Это описано в видео WWDC 2014 "Что нового в уведомлениях iOS"

Ответ 2

Для iOS < 10

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    //-- Set Notification
    if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) 
    {
           // iOS 8 Notifications
           [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

           [application registerForRemoteNotifications];
    }
    else
    {
          // iOS < 8 Notifications
          [application registerForRemoteNotificationTypes:
                     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
    }

     //--- your custom code
     return YES;
}

Для iOS10

fooobar.com/questions/31219/...

Ответ 3

Основываясь на ответе @Prasath. Так вы делаете это в Swift:

if application.respondsToSelector("isRegisteredForRemoteNotifications")
{
    // iOS 8 Notifications
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: (.Badge | .Sound | .Alert), categories: nil));
    application.registerForRemoteNotifications()
}
else
{
    // iOS < 8 Notifications
    application.registerForRemoteNotificationTypes(.Badge | .Sound | .Alert)
}

Ответ 4

iOS 8 изменил регистрацию уведомлений по-совместимому без обратной связи. Хотя вам нужно поддерживать iOS 7 и 8 (и пока приложения, созданные с помощью 8 SDK, не принимаются), вы можете проверить, какие селектор вам нужен, и условно назвать их правильно для текущей версии.

Здесь категория на UIApplication, которая скроет эту логику за чистым интерфейсом для вас, который будет работать как в Xcode 5, так и в Xcode 6.

Заголовок:

//Call these from your application code for both iOS 7 and 8
//put this in the public header
@interface UIApplication (RemoteNotifications)

- (BOOL)pushNotificationsEnabled;
- (void)registerForPushNotifications;

@end

Реализация:

//these declarations are to quiet the compiler when using 7.x SDK
//put this interface in the implementation file of this category, so they are
//not visible to any other code.
@interface NSObject (IOS8)

- (BOOL)isRegisteredForRemoteNotifications;
- (void)registerForRemoteNotifications;

+ (id)settingsForTypes:(NSUInteger)types categories:(NSSet*)categories;
- (void)registerUserNotificationSettings:(id)settings;

@end

@implementation UIApplication (RemoteNotifications)

- (BOOL)pushNotificationsEnabled
{
    if ([self respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
    {
        return [self isRegisteredForRemoteNotifications];
    }
    else
    {
        return ([self enabledRemoteNotificationTypes] & UIRemoteNotificationTypeAlert);
    }
}

- (void)registerForPushNotifications
{
    if ([self respondsToSelector:@selector(registerForRemoteNotifications)])
    {
        [self registerForRemoteNotifications];

        Class uiUserNotificationSettings = NSClassFromString(@"UIUserNotificationSettings");

        //If you want to add other capabilities than just banner alerts, you'll need to grab their declarations from the iOS 8 SDK and define them in the same way.
        NSUInteger UIUserNotificationTypeAlert   = 1 << 2;

        id settings = [uiUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert categories:[NSSet set]];            
        [self registerUserNotificationSettings:settings];

    }
    else
    {
        [self registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert];
    }
}

@end

Ответ 5

Я думаю, что это лучший способ сохранить обратную совместимость, если мы пойдем с этим подходом, он работает для моего дела, и надежда будет работать на вас. Также довольно легко понять.

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

Ответ 6

Для Swift-inclined:

if let registration: AnyObject = NSClassFromString("UIUserNotificationSettings") { // iOS 8+
    let notificationTypes: UIUserNotificationType = (.Alert | .Badge | .Sound)
    let notificationSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)

    application.registerUserNotificationSettings(notificationSettings)
} else { // iOS 7
    application.registerForRemoteNotificationTypes(.Alert | .Badge | .Sound)
}

Ответ 7

Я не мог понять, к какой переменной "категорий" NSSet нужно установить, поэтому, если кто-то может меня заполнить, я с удовольствием отредактирую этот пост. Однако приведенное ниже сообщение вызывает диалоговое окно push-уведомления.

[[UIApplication sharedApplication] registerForRemoteNotifications];
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];

Изменить: у меня есть push-уведомление для отправки на мой телефон с этим кодом, поэтому я не уверен, что параметр категорий необходим.

Ответ 8

Итак, получается, что поскольку AnyObject является духовным преемником id, вы можете вызвать любое сообщение, которое вы хотите на AnyObject. Это эквивалент отправки сообщения в id. Хорошо, справедливо. Но теперь мы добавляем в понятие, что все методы являются необязательными для AnyObject, и с ними мы можем работать.

Учитывая вышеизложенное, я надеялся, что могу просто передать UIApplication.sharedApplication() в AnyObject, затем создать переменную, равную сигнатуре метода, установить эту переменную в необязательный метод, а затем проверить переменную. Кажется, это не работало. Я предполагаю, что при компиляции с iOS 8.0 SDK компилятор знает, где он думает, что этот метод должен быть, поэтому он оптимизирует все это до поиска в памяти. Все работает нормально, пока не попытаюсь проверить переменную, после чего я получаю EXC_BAD_ACCESS.

Однако в том же разговоре WWDC, где я нашел камень, о том, что все методы являются необязательными, они используют опциональную цепочку для вызова необязательного метода - и это, похоже, работает. Хромой частью является то, что вы должны фактически попытаться вызвать метод, чтобы узнать, существует ли он, который в случае регистрации для уведомлений является проблемой, потому что вы пытаетесь выяснить, существует ли этот метод до того, как вы создадите Объект UIUserNotificationSettings. Кажется, что вызов этого метода с нулем, но все в порядке, поэтому решение, которое, кажется, работает для меня, это:

var ao: AnyObject = UIApplication.sharedApplication()
if let x:Void = ao.registerUserNotificationSettings?(nil) {
    // It iOS 8
    var types = UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert
    var settings = UIUserNotificationSettings(forTypes: types, categories: nil)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
} else {
    // It older
    var types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound | UIRemoteNotificationType.Alert
    UIApplication.sharedApplication().registerForRemoteNotificationTypes(types)
}

После долгих поисков, связанных с этим, ключевая информация пришла из этого сообщения WWDC https://developer.apple.com/videos/wwdc/2014/#407 прямо посередине в разделе "Необязательный Методы в протоколах"

В Xcode 6.1 beta приведенный выше код больше не работает, код ниже работает:

   if UIApplication.sharedApplication().respondsToSelector("registerUserNotificationSettings:") {
        // It iOS 8
        var types = UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert
       var settings = UIUserNotificationSettings(forTypes: types, categories: nil)
       UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    } else {
        // It older
        var types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound | UIRemoteNotificationType.Alert
        UIApplication.sharedApplication().registerForRemoteNotificationTypes(types)
    }

Ответ 9

Если вы хотите добавить поддержку IOS7 IOS8, вы можете применить этот код к своему проекту.

-(void) Subscribe {
    NSLog(@"Registering for push notifications...");

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
}

-(void)application:(UIApplication *)application 
    didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {

    if (notificationSettings.types) {
        NSLog(@"user allowed notifications");
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        NSLog(@"user did not allow notifications");
        UIAlertView *alert =[[UIAlertView alloc] 
            initWithTitle:@"Please turn on Notification"
            message:@"Go to Settings > Notifications > App.\n Switch on Sound, Badge & Alert"
            delegate:self
            cancelButtonTitle:@"Ok"
            otherButtonTitles: nil];
        [alert show];
        // show alert here
    }
}

Ответ 10

После Xcode 6.1 Beta приведен код ниже, небольшое изменение в коде Tom S, которое перестало работать с бета-версией 6.1 (работало с предыдущей бета-версией):

   if UIApplication.sharedApplication().respondsToSelector("registerUserNotificationSettings:") {
        // It iOS 8
        var types = UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert
       var settings = UIUserNotificationSettings(forTypes: types, categories: nil)
       UIApplication.sharedApplication().registerUserNotificationSettings(settings)
    } else {
        // It older
        var types = UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound | UIRemoteNotificationType.Alert
        UIApplication.sharedApplication().registerForRemoteNotificationTypes(types)
    }

Ответ 11

Вы можете использовать этот

if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) 
    {
        // for iOS 8
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

        [application registerForRemoteNotifications];
    }
    else
    {
        // for iOS < 8
        [application registerForRemoteNotificationTypes:
         (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
    }

    // RESET THE BADGE COUNT 
    application.applicationIconBadgeNumber = 0;

Ответ 12

Swift 2.0

// Checking if app is running iOS 8
    if application.respondsToSelector("isRegisteredForRemoteNotifications") {

        print("registerApplicationForPushNotifications - iOS 8")

        application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil));
        application.registerForRemoteNotifications()

    } else {
        // Register for Push Notifications before iOS 8
        print("registerApplicationForPushNotifications - <iOS 8")
        application.registerForRemoteNotificationTypes([UIRemoteNotificationType.Alert, UIRemoteNotificationType.Badge, UIRemoteNotificationType.Sound])

    }

Ответ 13

Если вам нужен код ios 8, это должно сделать это.

 - (BOOL)application:(UIApplication *)application       didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
       [application registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound  | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)  categories:nil]];

       [application registerForRemoteNotifications];
}

 return YES;
}

Ответ 14

Это более чистый способ, который я делаю, и он просто отлично работает

if (floor(NSFoundationVersionNumber) < NSFoundationVersionNumber_iOS_8_0)
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|
     UIRemoteNotificationTypeAlert| UIRemoteNotificationTypeSound];
     else {
         [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 
         [application registerForRemoteNotifications];
     }

Ответ 15

для iOS 8 и выше

UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert) categories:nil];
[application registerUserNotificationSettings:settings];