iOS-应用程序处于后台时异步

iOS-应用程序处于后台时异步,ios,swift,Ios,Swift,我正在尝试运行一个简单的iOS应用程序,在指定时间后将通知推送到用户屏幕 到目前为止,这是我所拥有的(从另一个线程借用): 我唯一的问题是aSync After不会在应用程序处于后台时运行 例如,如果用户进入锁屏或其他应用程序,则不会触发通知 谁能给我一个建议,让我如何做到这一点 谢谢!:) 方法: 按时间间隔使用UNNotificationRequest 以下提到的解决方案将在以下情况下工作: 前景 背景 应用程序已关闭 步骤: 设置代理(在前台发出警报) 向用户请求授权以获得警报 创

我正在尝试运行一个简单的iOS应用程序,在指定时间后将通知推送到用户屏幕

到目前为止,这是我所拥有的(从另一个线程借用):

我唯一的问题是aSync After不会在应用程序处于后台时运行

例如,如果用户进入锁屏或其他应用程序,则不会触发通知

谁能给我一个建议,让我如何做到这一点

谢谢!:)

方法:
  • 按时间间隔使用
    UNNotificationRequest
  • 以下提到的解决方案将在以下情况下工作:
    • 前景
    • 背景
    • 应用程序已关闭
步骤:
  • 设置代理(在前台发出警报)
  • 向用户请求授权以获得警报
  • 创建通知
  • 将其添加到通知中心
  • AppDelegate:
    AppDelegate
    必须符合
    UNUserNotificationCenterDelegate

    将通知中心的委托设置为
    AppDelegate

    import UserNotifications
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
    
            UNUserNotificationCenter.current().delegate = self
    
            return true
        }
    
        //MARK: UNUserNotificationCenterDelegate
    
        //This is required to be alerted when app is in foreground
        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    willPresent notification: UNNotification,
                                    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            print("will present")
            completionHandler([.alert, .badge, .sound])
        }
    
        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    didReceive response: UNNotificationResponse,
                                    withCompletionHandler completionHandler: @escaping () -> Void) {
            print("did receive")
        }
    }
    
    设置通知:
    您不能在后台执行任意的、基于计时器的代码。您需要通过在
    触发器中指定所需的发送时间来安排通知的发送,但这并不能回答问题。具体来说,它没有显示如何在后台延迟执行任务(不是通知)
    
    import UserNotifications
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
    
            UNUserNotificationCenter.current().delegate = self
    
            return true
        }
    
        //MARK: UNUserNotificationCenterDelegate
    
        //This is required to be alerted when app is in foreground
        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    willPresent notification: UNNotification,
                                    withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            print("will present")
            completionHandler([.alert, .badge, .sound])
        }
    
        func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    didReceive response: UNNotificationResponse,
                                    withCompletionHandler completionHandler: @escaping () -> Void) {
            print("did receive")
        }
    }
    
    import UserNotifications
    
    private func setupNotification() {
    
        requestAuthorization { [weak self] isGranted, error in
    
            if let error = error {
    
                print("Request Authorization Error: \(error)")
                return
            }
    
            guard isGranted else {
                print("Authorization Denied")
                return
            }
    
            self?.addNotification()
        }
    }
    
    private func requestAuthorization(completionBlock: @escaping (Bool, Error?) -> ()) {
    
        let center = UNUserNotificationCenter.current()
    
        center.requestAuthorization(options: [.alert, .badge, .sound]) { isGranted, error in
    
            completionBlock(isGranted, error)
        }
    }
    
    
    private func addNotification() {
    
        let content = UNMutableNotificationContent()
    
        content.title = "Testing Notification"
        content.body = "This is a test for notifications"
        content.sound = .default()
    
        let timeInterval = TimeInterval(5)
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: timeInterval, repeats: false)
    
        let request = UNNotificationRequest(identifier: "Something",
                                            content: content,
                                            trigger: trigger)
    
    
        let center = UNUserNotificationCenter.current()
    
        center.add(request) { error in
    
            if let error = error {
                print("Error adding notification request: \(error)")
            }
            else {
                print("Successfully added notification request")
            }
        }
    }