当应用程序在后台运行时,为什么通知操作不起作用?

时间:2020-06-27 18:48:57

标签: swift xcode unusernotificationcenter

所以,我有一个发送本地通知的应用,并且在应用委托中定义了一个通知类别

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    application.applicationIconBadgeNumber = 0
    
    UNUserNotificationCenter.current().delegate = self
    
    let StartAction = UNNotificationAction(identifier: "start", title: "Start", options: [.foreground])
   
    let category = UNNotificationCategory(identifier: "StartTap", actions: [StartAction], intentIdentifiers: [], options: [])
  
    
    UNUserNotificationCenter.current().setNotificationCategories([category])
    
    return true
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        UserDefaults.standard.set(true, forKey: "StartTap")
        let sb = UIStoryboard(name: "Main", bundle: nil)
        let startVC = sb.instantiateViewController(withIdentifier: "startVC") as! StartViewController
        window?.rootViewController = startVC
        
}

问题是当应用程序被强制关闭而不在后台关闭时,它可以工作,但是当应用程序在后台关闭时,什么也没有发生,而我只是进入默认屏幕。我该怎么做才能解决此问题?

0 个答案:

没有答案