下面的方法总是返回相同的值:
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
每次运行此代码时,结果都是肯定的。即使当我进入“设置”应用程序并将我的应用程序的推送通知设置为“关闭”时,当上面的代码运行时,它的计算结果也是YES。
其他详细信息:*我在一部装有iOS 8.1.3的苹果手机上运行应用程序*我在Xcode6.1中运行应用程序,并且我已经将手机物理连接到我的机器上
你知道为什么"isRegisteredForRemoteNotifications“的值不变吗?
发布于 2015-02-11 04:30:39
因为即使用户选择退出推送,iOS 8也会注册设备并提供令牌。
在这种情况下,当推送被发送时,推送不会呈现给用户,但如果你的应用程序正在运行,它会获得有效负载,这样你就可以在应用程序运行时更新它……
要检查是否在iOS 8中启用了推送通知,您应该检查启用的用户通知类型:
- (BOOL)pushNotificationsEnabled {
if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {
UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
return (types & UIUserNotificationTypeAlert);
}
else {
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
return (types & UIRemoteNotificationTypeAlert);
}
}
发布于 2015-11-06 05:39:10
如果您使用的是Swift 2,则按位运算符不适用于UIUserNotificationType。这是一个使用Swift 2的解决方案,iOS 8+:
func hasPushEnabled() -> Bool {
//ios 8+
if UIApplication.sharedApplication().respondsToSelector("currentUserNotificationSettings") == true {
let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
if (settings?.types.contains(.Alert) == true){
return true
}
else {
return false
}
}
else {
let types = UIApplication.sharedApplication().enabledRemoteNotificationTypes()
if types.contains(.Alert) == true {
return true
}
else {
return false
}
}
https://stackoverflow.com/questions/28441007
复制相似问题