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

Posted

技术标签:

【中文标题】如果 registerForRemoteNotificationTypes: 在 iOS 8.0 及更高版本中不支持,则为 iOS 构建【英文标题】:Building for iOS if registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later 【发布时间】:2014-08-05 20:10:55 【问题描述】:

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

【问题讨论】:

为什么不能使用 xcode 6 测试版? 我们可以使用 xcode 6 beta 并提交到应用商店吗?我的印象是 Apple 不会接受来自 xcode 测试版的构建。 该方法不适用于 ios8,但仍然适用于 ios 7。构建 xcode 6 beta,然后当 GA 版本出来时,以这种方式编译并提交。您是正确的,您只能在 sdk 退出测试版后提交 ios 8 应用程序。现有用户将继续收到推送通知,因为我相信他们的令牌不会失效 【参考方案1】:

iOS 8 更改了通知注册。所以你需要检查设备版本,然后你需要注册通知设置。(请查看this链接。) 我在 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;

【讨论】:

嗨@Julldar 感谢您的评论。我编辑我的内容。请编辑您的建议。因为这段代码有效。 我从来没有说过它不起作用:)。实际上,我只是要求您进行一些澄清,您这样做了,所以太好了:)。我实际上会删除另一条评论 :) 和这条评论,以免聚集 SO【参考方案2】:

您可能需要考虑使用 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)];

【讨论】:

【参考方案3】:
#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

【讨论】:

应该是UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge【参考方案4】:

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

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

【讨论】:

【参考方案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】:

你可以用这个,

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;

【讨论】:

【参考方案7】:

在 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;

【讨论】:

以上是关于如果 registerForRemoteNotificationTypes: 在 iOS 8.0 及更高版本中不支持,则为 iOS 构建的主要内容,如果未能解决你的问题,请参考以下文章

未正确调用 registerForRemoteNotifications 方法

registerForRemoteNotifications 仅适用于安装了 iOS 7 的 iPhone

@react-native-firebase/messaging : TypeError: (0 , _messaging.default)(...).registerForRemoteNotific

如何在 Swift 中同时为 iOS7 和 iOS8 调用 registerForRemoteNotifications?

iOS registerForRemoteNotifications 不会生成错误,但不会触发提供设备令牌的委托 [重复]

ios7 registerForRemoteNotificationTypes 没有提示询问用户权限