我正在尝试检测iOS 8中该应用的本地通知设置
对于UIUserNotificationSettings
,当我打开所有徽章,声音和警报时,它会返回7。
在该设置中,我关闭了“允许通知”,该应用仍然向我返回7,用于UIUserNotificationSettings(打开徽标,声音和警报)。有没有办法检测“允许Notification
”的开/关?
- (void)application:(UIApplication *)application
didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
NSLog(@"---notificationSettings.types %d" , notificationSettings.types );
if(notificationSettings.types!=7){
UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Please turn on Notification"
message:@"Go to Settings > Notifications > App.\n Switch on Sound, Badge & Alert"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles: nil];
[alert show];
}
}
自iOS8起不推荐使用方法enabledRemoteNotificationTypes
。
要在iOS8中查看远程通知状态,可以致电
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
如果用户在“设置”中禁用通知,它将返回NO。 Documentation上的isRegisteredForRemoteNotifications
或者您可以检索所有当前的通知设置:
[[UIApplication sharedApplication] currentUserNotificationSettings];
Documentation上的[currentUserNotificationSettings
- (BOOL)isUserNotificationAllowed {
UIUserNotificationType types = [[UIApplication sharedApplication] currentUserNotificationSettings].types;
if(types & UIUserNotificationTypeBadge || types & UIUserNotificationTypeSound || types & UIUserNotificationTypeAlert){
return YES;
}
else {
return NO;
}
}
import Foundation
import UserNotifications
class NotificaionStatusCheck {
var window: UIWindow?
private var currentViewController : UIViewController? = nil
static let shared = NotificaionStatusCheck()
public func currentViewController(_ vc: UIViewController?) {
self.currentViewController = vc
checkNotificationsAuthorizationStatus()
}
private func checkNotificationsAuthorizationStatus() {
let userNotificationCenter = UNUserNotificationCenter.current()
userNotificationCenter.getNotificationSettings { (notificationSettings) in
switch notificationSettings.authorizationStatus {
case .authorized:
print("The app is authorized to schedule or receive notifications.")
case .denied:
print("The app isn't authorized to schedule or receive notifications.")
self.NotificationPopup()
case .notDetermined:
print("The user hasn't yet made a choice about whether the app is allowed to schedule notifications.")
self.NotificationPopup()
case .provisional:
print("The application is provisionally authorized to post noninterruptive user notifications.")
self.NotificationPopup()
}
}
}
private func NotificationPopup(){
let alertController = UIAlertController(title: "Notification Alert", message: "Please Turn on the Notification to get update every time the Show Starts", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
})
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(settingsAction)
DispatchQueue.main.async {
self.currentViewController?.present(alertController, animated: true, completion: nil)
}
}
}
要在ViewController上访问此代码,请在viewDidLoad中使用此代码
NotificaionStatusCheck.shared.currentViewController(self)
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone)
// Yes it is..
希望,对您有帮助。.>
Swift 3 +
let isRegisteredForLocalNotifications = UIApplication.shared.currentUserNotificationSettings?.types.contains(UIUserNotificationType.alert) ?? false
Swift 2.3
let isRegisteredForLocalNotifications = UIApplication.sharedApplication().currentUserNotificationSettings()?.types.contains(UIUserNotificationType.Alert) ?? false
如果在错误的位置,我对此答案/评论表示歉意。我真的是iOS编程的新手,以前从未发布过堆栈溢出信息。我认为这实际上应该是评论,但如果没有50分,我是不允许的。如果解释还很初级,我也表示歉意,但再次重申一下:)。
我还希望测试用户是否更改了初始请求后我的应用允许/要求的通知。在尝试破译Apple文档之后(Apple的作者比我聪明得多,或者该文档故意模糊了),并做了一些测试,测试了
的值[[UIApplication sharedApplication] currentUserNotificationSettings].hash.
我相信这将返回一个三位哈希值,其中位1用于横幅通知,位2用于声音通知,位3用于警报通知。
所以...
000 = 0 = no notifications.
001 = 1 = only banner,
010 = 2 = only sound,
011 = 3 = sound and banner, no alert
100 = 4 = only alert notifications
and so on until,
111 = 7 = all notifications on.
如果在[设置]应用程序中关闭了[[允许通知,则也会显示0。希望这会有所帮助。
// Obj-C
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
// Swift
UIApplication.sharedApplication().isRegisteredForRemoteNotifications
如果您的应用程序可以接收远程通知,则返回这将向用户显示一个对话框,他们可以在其中允许或拒绝您的请求。不管他们的决定,该应用程序仍将能够接收远程通知。true
。但是receiving远程通知并不意味着它也将display通知给用户。
几乎在每种情况下,请求registerForRemoteNotifications()
都会成功,并且调用AppDelegate的didRegisterForRemoteNotificationsWithDeviceToken
会为您提供设备令牌。然后,该应用程序可以接收未显示给用户的silent
远程通知。例如,您可以在收到此类通知时在应用程序中触发后台进程。参见Documentation。不仅要向您请求权限的用户接收而且要向[远程通知:
// Swift let notificatonSettings = UIUserNotificationSettings(forTypes: [.Badge, .Alert, .Sound], categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(notificatonSettings)
如果用户允许,则调用AppDelegate的didRegisterForRemoteNotificationsWithDeviceToken
。要检查用户是否允许或拒绝您的请求,或者实际上是稍后在您致电的iOS设置中更改了通知权限:
// Obj-C [[UIApplication sharedApplication] currentUserNotificationSettings]; // Swift UIApplication.sharedApplication().currentUserNotificationSettings()
请参见Documentation。
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings: (UIUserNotificationSettings *)notificationSettings
{
if (notificationSettings.types) {
NSLog(@"user allowed notifications");
[[UIApplication sharedApplication] registerForRemoteNotifications];
}else{
NSLog(@"user did not allow notifications");
// show alert here
}
}
在连续发射时,使用:
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))){
if(UIApplication.sharedApplication().currentUserNotificationSettings().hashValue == 0){
pushNotificationStatus = "false"
} else {
pushNotificationStatus = "true"
}
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
if (notificationSettings.types == .None){ // User did NOT allowed notifications
}else{ // User did allowed notifications
}
}
从任何其他ViewController:
if UIApplication.sharedApplication().currentUserNotificationSettings()!.types.contains(.None){
}else{
}
func notificationsAreOk() -> Bool {
let wishedTypes = UIUserNotificationType.Badge |
UIUserNotificationType.Alert |
UIUserNotificationType.Sound;
let application = UIApplication.sharedApplication()
let settings = application.currentUserNotificationSettings()
if settings == nil {
return false
}
if settings.types != wishedTypes {
return false
}
return true
}
EDIT:禁用通知后,某些测试后并不总是有效。我正在考虑发送测试通知,以了解它们的工作时间。