gpt4 book ai didi

ios - 如何根据 iOS 版本处理推送通知?

转载 作者:行者123 更新时间:2023-11-28 19:56:58 25 4
gpt4 key购买 nike

对于 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 设备上使用这个新代码会导致应用程序崩溃,因此我需要让代码确定手机的版本,并运行适当的推送通知代码。我怎样才能让应用程序检查它并使用正确的应用程序?

最佳答案

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

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。

关于ios - 如何根据 iOS 版本处理推送通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25953484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com