如果 registerForRemoteNotificationTypes: 在 iOS 8.0 及更高版本中不受支持,则为 iOS 构建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25147454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:43:43  来源:igfitidea点击:

Building for iOS if registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later

iosxcodeapple-push-notificationsios8xcode6

提问by Jason Hocker

If there are breaking changes with how devices register for notifications, and we cannot use registerForRemoteNotificationTypes: anymore, how can we build a new version of the app to support iOS 8 if we cannot use Xcode 6 beta? Will we have to build and submit the day the Xcode 6 GM version is released for our users to continue to get push notifications?

如果设备注册通知的方式发生重大变化,并且我们不能再使用 registerForRemoteNotificationTypes: 了,如果我们不能使用 Xcode 6 beta,我们如何构建新版本的应用程序来支持 iOS 8?我们是否必须在 Xcode 6 GM 版本发布之日构建和提交我们的用户才能继续获得推送通知?

回答by Ra?it ERASLAN

iOS 8 has changed notification registration. So you need to check device version and then you need to register notification settings.(please check thislink.) I try this code on Xcode 6 and its worked for me.

iOS 8 更改了通知注册。所以你需要检查设备版本,然后你需要注册通知设置。(请检查这个链接。)我在 Xcode 6 上尝试了这段代码,它对我有用。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
        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)];
        }

     return YES;
}

回答by alex da franca

You might want to consider using respondsToSelector rather than checking the system version:

您可能需要考虑使用 RespondsToSelector 而不是检查系统版本:

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

回答by Muhammad Adil

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    [[UIApplication sharedApplication] registerForRemoteNotifications];
#else
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#endif

回答by Hardik Bar

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)];
}

回答by VoidStack

As per Apple documentation registerForRemoteNotificationTypes:is deprecated in iOS 8 instead you can use registerForRemoteNotificationsand registerUserNotificationSettings:.

根据 Apple 文档registerForRemoteNotificationTypes:在 iOS 8 中已弃用,您可以使用registerForRemoteNotificationsregisterUserNotificationSettings:

Xcode 6 beta and iOS 8 beta are pre-release softwar. Beta versions are for development and testing only. New apps and app updates must be built with release versions of Xcode and iOS to submit to the App Store.

Xcode 6 beta 和 iOS 8 beta 是预发布软件。Beta 版本仅用于开发和测试。新的应用程序和应用程序更新必须使用 Xcode 和 iOS 的发布版本构建才能提交到 App Store。

回答by Neenu

You can use this,

你可以用这个,

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;

回答by amit gupta

write this code in AppDelegate didFinishLaunchingWithOptions function

在 AppDelegate didFinishLaunchingWithOptions 函数中编写此代码

 if([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)]) {
UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge;
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
 }
        else {
UIRemoteNotificationType types = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
  }    [self.window makeKeyAndVisible];
return YES;