Change push notifications programmatically in Swift

前端 未结 4 2108
天涯浪人
天涯浪人 2021-01-05 16:08

I have an app where I have implemented push notifications. I check with the user to allow remote notifications with:

let settings = UIUserNotificationSetting         


        
4条回答
  •  粉色の甜心
    2021-01-05 16:35

    if settings.authorizationStatus != .authorized{
    
                // Either denied or notDetermined
                // Ask the user to enable it in settings.
    
                UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
                    (granted, error) in
                      // add your own
                    UNUserNotificationCenter.current().delegate = self
                    let alertController = UIAlertController(title: appName, message: "Please enable notifications in settings and try again.", 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 {
                        (UIApplication.shared.delegate as? AppDelegate)?.window?.rootViewController?.present(alertController, animated: true, completion: nil)
                    }
                }
            }
    

提交回复
热议问题