我有以下代码,我用它来决定用户是否已授予本地通知权限:
UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (grantedSettings.types == UIUserNotificationTypeNone) {
NSLog(@"No permission granted");
//IF AND ONLY IF alerts are wanted by raymio user! FIX
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Notice"
message:@"You need to enable notifications to be able to receive sun alerts!"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* OKAction = [UIAlertAction actionWithTitle:@"Go to Settings now" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}];
[alert addAction:OKAction];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:cancelAction];
[self.window addSubview:self.window.rootViewController.view];
[self.window makeKeyAndVisible];
[self.window.rootViewController presentViewController:alert animated:YES completion:nil];
}
else if (grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert ){
NSLog(@"Sound and alert permissions ");
}
else if (grantedSettings.types & UIUserNotificationTypeAlert){
NSLog(@"Alert Permission Granted");
基本上,它只是不起作用。当应用程序设置中的允许通知开关明确设置为关闭时,我获得“警告权限授予”。如果我将其切换为开启,则设置也不会改变。以下是grantSettings的控制台输出。它保持不变。我还有其他事情发生了。现在我只想删除代码。我必须首先执行此操作,以防用户在初始提示时意外按下取消(并在应用程序中请求警报)。
granted settings: <UIUserNotificationSettings: 0x17042e940; types: (UIUserNotificationTypeAlert UIUserNotificationTypeBadge UIUserNotificationTypeSound);>
这个有缺陷的区域有什么见解?顺便说一下,我正在运行8.1。对ios9的需求并不完全相同,因为ios9会让用户多次提示通知权限。
答案 0 :(得分:0)
我在我的一个项目中使用了以下方法来确定用户是否已经授予权限,或者他/她是否实际关闭了设置中的通知。将此方法放在Appdelegate中并检查
A | B | .. n
------------------------------------------
uR96BDuI8YR7gqbc | lTvW5TcIVIVNmE4_LYQ4eA
NA | 2DT4Lxjyt3F1ZJt8Mt0o4Q
NA | NA
.. | NA
NA | NA
然后你可以用条件进行简单的检查
example <- structure(
list(
A = structure(
list(coln = "A", id = "uR96BDuI8YR7gqbc-Ncg9A"), .Names = c("ALPHA",
"ID")
),
B = structure(
list(
coln = "B",
id = c("lTvW5TcIVIVNmE4_LYQ4eA", "2DT4Lxjyt3F1ZJt8Mt0o4Q")
), .Names = c("BETA", "ID")
),
C = structure(
list(cuisine = "C", id = character(0)), .Names = c("CETA",
"ID")
),
D = structure(
list(cuisine = "D",
id = character(0)), .Names = c("ALPHA", "ID")
),
E = structure(
list(cuisine = "E", id = "O1NBNVfXWmk3CvszAhOEoQ"), .Names = c("ALPHA",
"ID")
),
F = structure(
list(cuisine = "F",
id = character(0)), .Names = c("ALPHA", "ID")
)
), .Names = c(
"A", "B", "C", "D",
"E", "F"
)
)
我希望这可以帮助你。
答案 1 :(得分:0)
在iOS 9中,要检查应用程序当前是否已注册远程通知,请使用:
BOOL isEnabled = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];