我正在寻找一种方法来确定用户是否有,通过设置,启用或禁用他们的推送通知我的应用程序。
当前回答
在iOS7和之前,你应该使用enabledRemoteNotificationTypes并检查它是否等于(或不等于取决于你想要什么)UIRemoteNotificationTypeNone。
然而,对于iOS8来说,仅仅检查isregisteredforremotenotifations并不总是足够的,因为上面有许多状态。你还应该检查application.currentUserNotificationSettings.types是否等于(或不等于取决于你想要什么)UIUserNotificationTypeNone!
isregisteredforremotenotifations可能返回true,即使currentUserNotificationSettings。types返回UIUserNotificationTypeNone。
其他回答
为了完成这个答案,它可以这样工作……
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
switch (types) {
case UIRemoteNotificationTypeAlert:
case UIRemoteNotificationTypeBadge:
// For enabled code
break;
case UIRemoteNotificationTypeSound:
case UIRemoteNotificationTypeNone:
default:
// For disabled code
break;
}
编辑:这是不对的。因为这些是位智慧的东西,它不会与开关工作,所以我结束使用这个:
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
UIRemoteNotificationType typesset = (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge);
if((types & typesset) == typesset)
{
CeldaSwitch.chkSwitch.on = true;
}
else
{
CeldaSwitch.chkSwitch.on = false;
}
在iOS7和之前,你应该使用enabledRemoteNotificationTypes并检查它是否等于(或不等于取决于你想要什么)UIRemoteNotificationTypeNone。
然而,对于iOS8来说,仅仅检查isregisteredforremotenotifations并不总是足够的,因为上面有许多状态。你还应该检查application.currentUserNotificationSettings.types是否等于(或不等于取决于你想要什么)UIUserNotificationTypeNone!
isregisteredforremotenotifations可能返回true,即使currentUserNotificationSettings。types返回UIUserNotificationTypeNone。
在尝试同时支持iOS8和更低版本时,我并没有像Kevin建议的那样使用isregisteredforremotenotifations。相反,我使用了currentUserNotificationSettings,它在我的测试中工作得很好。
+ (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;
}
re:
这是正确的
if (types & UIRemoteNotificationTypeAlert)
但是下面也是正确的!(因为UIRemoteNotificationTypeNone是0)
if (types == UIRemoteNotificationTypeNone)
参见以下内容
NSLog(@"log:%d",0 & 0); ///false
NSLog(@"log:%d",1 & 1); ///true
NSLog(@"log:%d",1<<1 & 1<<1); ///true
NSLog(@"log:%d",1<<2 & 1<<2); ///true
NSLog(@"log:%d",(0 & 0) && YES); ///false
NSLog(@"log:%d",(1 & 1) && YES); ///true
NSLog(@"log:%d",(1<<1 & 1<<1) && YES); ///true
NSLog(@"log:%d",(1<<2 & 1<<2) && YES); ///true
quantumpotato的问题:
类型是由
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
可以使用
if (types & UIRemoteNotificationTypeAlert)
而不是
if (types == UIRemoteNotificationTypeNone)
将允许你只检查通知是否启用(不用担心声音,徽章,通知中心等)。第一行代码(types & UIRemoteNotificationTypeAlert)如果“Alert Style”设置为“横幅”或“警报”将返回YES,如果“Alert Style”设置为“None”则返回NO,与其他设置无关。
推荐文章
- registerForRemoteNotificationTypes: iOS 8.0及以上版本不支持
- Xcode 4 -在新的Macintosh安装上的配置文件上“没有找到有效的签名标识”错误
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- 从父iOS访问容器视图控制器
- 自定义dealloc和ARC (Objective-C)
- 调整UITableView的大小以适应内容
- 在代码中为UIButton设置一个图像
- NSRange从Swift Range?
- UICollectionView中的单元格间距
- 我如何在我的iOS应用程序中每n分钟得到一个后台位置更新?
- 如何使用iOS创建GUID/UUID
- 禁用所呈现视图控制器的交互式撤销