Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS中打开和关闭通知?_Ios_Swift_Unusernotificationcenter_Usernotifications - Fatal编程技术网

如何在iOS中打开和关闭通知?

如何在iOS中打开和关闭通知?,ios,swift,unusernotificationcenter,usernotifications,Ios,Swift,Unusernotificationcenter,Usernotifications,我正在尝试在iOS 11应用程序中实现设置屏幕,我需要一个用于控制用户通知的UI开关。当设置为on off时,我想放弃对通知的权限;当设置为on时,我想请求权限(要求用户有权发送通知的标准对话框) 要请求许可,我发现以下代码: UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in // Enable or disable featur

我正在尝试在iOS 11应用程序中实现设置屏幕,我需要一个用于控制用户通知的UI开关。当设置为on off时,我想放弃对通知的权限;当设置为on时,我想请求权限(要求用户有权发送通知的标准对话框)

要请求许可,我发现以下代码:

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in
    // Enable or disable features based on authorization.
}
但是,如果我在“系统设置”中关闭应用程序的通知,则此代码不会弹出带有请求的对话框,它只会在
grated
中返回false

我找不到任何关于如何放弃权限的信息

有没有关于如何解决这个问题的提示?这是可能的,还是苹果认为这项任务应该只由系统设置来完成

在iOS中,打开/关闭权限推送通知仅出现一次。 因此,为了实现这一点,你需要尽可能地做一些调整 首先检查通知是否已启用

之后,您可以使用打开/关闭按钮创建自定义弹出窗口 并导航到系统设置页面,用户可以在其中启用该选项 相应地

适用于iOS 10.0及以上版本

  UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        if settings.authorizationStatus == .authorized {
            // Notifications are allowed
        }
        else {
            // Either denied or notDetermined

            let alertController = UIAlertController(title: nil, message: "Do you want to change notifications settings?", preferredStyle: .alert)

            let action1 = UIAlertAction(title: "Settings", style: .default) { (action:UIAlertAction) in
                if let appSettings = NSURL(string: UIApplication.openSettingsURLString) {
                    UIApplication.shared.open(appSettings as URL, options: [:], completionHandler: nil)
                }
            }

            let action2 = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction) in
            }

            alertController.addAction(action1)
            alertController.addAction(action2)
            self.present(alertController, animated: true, completion: nil)
        }
    }

仅限系统设置。原因很简单,用户不会在每次应用启动时收到“推送通知请求”。一旦您获得发送通知的权限,您就必须自行决定是否要向用户发送实际通知,或者添加用户可以选择加入/退出的主题。因此,无法通过编程方式放弃权限?为什么要这样做?我不知道有一种方法,那就是没有合理的用例,因为决策必须是用户的,而且只能是用户的。您不应该为用户决定是否继续接收通知。此外,设置这样的设置毫无意义,因为作为程序员,您可以随时停止发送通知,从应用程序的角度来看,这与关闭通知是一样的。通知开关是在应用程序中还是在系统中对于这个特定问题没有区别。因为您要求用户授予您权限,所以要求他撤销权限没有任何意义。您的应用程序可以决定不再向他发送通知,但这完全在您的掌握之中,用户不应受到此决定的影响。谢谢您的帮助。
if let appSettings = NSURL(string: UIApplicationOpenSettingsURLString) {
    UIApplication.shared.openURL(appSettings as URL)
}
  UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        if settings.authorizationStatus == .authorized {
            // Notifications are allowed
        }
        else {
            // Either denied or notDetermined

            let alertController = UIAlertController(title: nil, message: "Do you want to change notifications settings?", preferredStyle: .alert)

            let action1 = UIAlertAction(title: "Settings", style: .default) { (action:UIAlertAction) in
                if let appSettings = NSURL(string: UIApplication.openSettingsURLString) {
                    UIApplication.shared.open(appSettings as URL, options: [:], completionHandler: nil)
                }
            }

            let action2 = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction) in
            }

            alertController.addAction(action1)
            alertController.addAction(action2)
            self.present(alertController, animated: true, completion: nil)
        }
    }