gpt4 book ai didi

ios - 实现从 iOS7 到 iOS 11 的推送通知。* 我必须处理所有三种情况吗?

转载 作者:行者123 更新时间:2023-11-28 23:52:28 24 4
gpt4 key购买 nike

我知道这听起来有些笨拙,但我已经习惯于为 APNs 集成第三方 SDK。

我有一个必须支持 iOS 7 及更高版本的旧版应用程序,该应用程序具有推送通知。据我了解,对于低于 iOS8、低于 iOS10 和不同的 iOS 10 及更高版本,我们有不同的方法来注册推送通知。所以我需要使用 if 条件检查操作系统版本吗

   if(ios10 and above){
// here register for iOS 10 and above

}else if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
// iOS 8 Notifications
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

[application registerForRemoteNotifications];
}
else {
// iOS < 8 Notifications
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}

上面的做法是对的还是我错了?

最佳答案

这是我使用的代码片段,供您引用:)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+ (void)registerAPNS {
if ([UIDevice currentDevice].systemVersion.floatValue >= 10.0f) {
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0f) {
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
[[UIApplication sharedApplication] registerForRemoteNotifications];
} else {
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeSound|UIRemoteNotificationTypeAlert];
}
}
#pragma clang diagnostic pop

关于ios - 实现从 iOS7 到 iOS 11 的推送通知。* 我必须处理所有三种情况吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51759385/

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