【问题标题】:Using Remote notification with deployment target 8.1 and XCODE 6.1对部署目标 8.1 和 XCODE 6.1 使用远程通知
【发布时间】:2015-03-11 17:11:14
【问题描述】:

我想对部署目标 8.1 和 xcode 6.1 使用远程通知
我正在使用以下代码

if ([application 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)];
}

但它在其他部分向我展示了deprecated 方法......所以现在我该如何摆脱它...... 我尝试的其他代码是:

【问题讨论】:

  • 你确定你也有作为部署目标的 ios7 或更低版本吗?而不是进行干净的构建并再次构建

标签: ios objective-c xcode6 apple-push-notifications


【解决方案1】:

您可以像这样忽略弃用警告:

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#pragma clang diagnostic pop

    }
#else
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
#endif

this post:

【讨论】:

    【解决方案2】:

    希望以下代码可以满足您的需求

    if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
    {
        // iOS 8 Notifications
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
    
        [application registerForRemoteNotifications];
    }
    else
    {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeNewsstandContentAvailability |
            UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }
    

    【讨论】:

    • 没有哥们..我用的一样的东西..但不起作用..谢谢
    • 您使用的是部署目标 8.1 和 xcode 6.1 吗??
    • 我会通过这个链接尝试一切...corinnekrych.blogspot.in/2014/07/…
    • @Bhavin 请复制并粘贴我的代码然后运行。
    • @SumitGarg 我发布了一张图片...查看了
    【解决方案3】:

    registerForRemoteNotificationTypes: 方法已从 iOS 8.0 中弃用。由于您使用 iOS 8.1 SDK 进行编译,您将收到警告。这是一种预期行为,您的应用程序将正常工作,因为当且仅当您的 iOS 版本低于 8.0 且定义了方法 registerUserNotificationSettings: 时,您的应用程序才会使用 registerForRemoteNotificationTypes:

    如果您需要避免任何折旧警告,您可以使用-Wno-deprecated-declarations 标志来抑制警告。

    【讨论】:

      【解决方案4】:

      好的。我可以理解您使用部署目标 8.1 的问题。 只需将部署目标更改为小于或等于 7,否则删除 else 部分,因为不需要在 8.1 目标中定义它

      【讨论】:

      • 我认为,如果部署目标是 8.1,那么它只支持使用 8.1 操作系统的 iphone……对吗??
      • @Bhavin 是 ios 8.1 及更高版本
      猜你喜欢
      • 2011-01-08
      • 2015-05-01
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-22
      • 2012-08-02
      相关资源
      最近更新 更多