【问题标题】:How to handle push notifications depending on iOS version?如何根据 iOS 版本处理推送通知?
【发布时间】:2014-09-20 21:30:41
【问题描述】:

对于 iOS7,Parse 在 AppDelegate 中使用以下代码处理推送通知:

[application registerForRemoteNotificationTypes:
 UIRemoteNotificationTypeBadge|
 UIRemoteNotificationTypeAlert|
 UIRemoteNotificationTypeSound];

registerForRemoteNotificationTypes 在 iOS8 中不受支持,而用于在 iOS8 中处理推送通知的新代码现在如下所示:

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

在 iOS7 设备上使用这个新代码会导致应用崩溃,因此我需要让代码确定手机的版本,并运行适当的推送通知代码。如何让应用检查这一点并使用正确的?

【问题讨论】:

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


【解决方案1】:

检查方法的可用性总是比检查操作系统版本更好。

if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerForRemoteNotifications)]) {

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    [[UIApplication sharedApplication] registerForRemoteNotifications];

} else {

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

假设您的部署目标 >= 7.0。

【讨论】:

    【解决方案2】:

    可能是registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later的副本

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

    【讨论】:

    • 最好的方法是添加一个带有指向另一个问题的链接的评论。或者,更好的是,将此问题标记为重复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    相关资源
    最近更新 更多