【发布时间】:2012-01-25 16:06:14
【问题描述】:
如何检查 iPhone 上是否启用了通知? 我安装了应用程序,应用程序说确认为应用程序启用推送通知,我单击确定。但如果在 iPhone 上禁用通知,则此操作不会启用通知。 如何检查?
【问题讨论】:
标签: iphone xcode push-notification apple-push-notifications
如何检查 iPhone 上是否启用了通知? 我安装了应用程序,应用程序说确认为应用程序启用推送通知,我单击确定。但如果在 iPhone 上禁用通知,则此操作不会启用通知。 如何检查?
【问题讨论】:
标签: iphone xcode push-notification apple-push-notifications
这应该有效:
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone)
// Disabled
【讨论】:
这在 iOS8 中发生了变化。支持iOS8及更低版本
+ (BOOL)notificationServicesEnabled {
BOOL isEnabled = NO;
if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){
UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (!notificationSettings || (notificationSettings.types == UIUserNotificationTypeNone)) {
isEnabled = NO;
} else {
isEnabled = YES;
}
} else {
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types & UIRemoteNotificationTypeAlert) {
isEnabled = YES;
} else{
isEnabled = NO;
}
}
return isEnabled;
}
【讨论】:
AppDelegate还是ViewController?
DeviceNotificationsManagerClass里面,里面封装了手机通知子系统相关的逻辑。这样外部世界(即应用程序的其余部分)只引用DeviceNotificationsManager,而不必详细了解 iOS 的通知系统。