【发布时间】:2017-05-24 16:32:57
【问题描述】:
我需要更新我们的应用程序以支持 iOS 9 和 iOS 10,所以我的问题是使用 UNUserNotificationCenter 进行推送通知。
所以在 iOS 9 中,我们有一个方法可以返回 UIUserNotificationSettings 的结果,比如
- (BOOL)alertEnabled {
UIUserNotificationSettings *theSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
return [theSettings types] & UIUserNotificationTypeAlert;
}
在 iOS 10 中,我做了类似的事情
- (void)userNotificationsAuthorization :(void (^)(BOOL alertIsActive))completion {
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
completion(settings.alertSetting == UNNotificationSettingEnabled);
}];
}
调用它并通过完成处理程序获取它。
我的问题:是否有可能使用 getNotificationSettingsWithCompletionHandler 和 return 值而不是完成处理程序,我可以将它用于我的 alertEnabled 方法?
非常感谢。
更新:
通过这种方法,它正在工作
- (BOOL)alertIsEnabled {
__block BOOL alertIsActive = NO;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
alertIsActive = settings.alertSetting == UNNotificationSettingEnabled;
dispatch_semaphore_signal(sem);
}];
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
}
else {
UIUserNotificationSettings *theSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
alertIsActive = [theSettings types] & UIUserNotificationTypeAlert;
}
return alertIsActive;
}
但也许有更好的解决方案
【问题讨论】:
标签: ios push-notification ios9 ios10