AppDelegate:仅当有新的通知时,呼叫功能,如何



自从我的应用以来,我遇到了问题,显然,每次应用程序完成时(在AppDelegate中(调用新的通知功能。如何仅调用newNotification()-函数,当它用于新通知时,而不是在应用程序开始启动时?

这是AppDelegate(MVC = MainViewController(的相关代码:

  let center = UNUserNotificationCenter.current()
    center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
    }
    let generalCategory = UNNotificationCategory(identifier: "GENERAL",
                                                 actions: [],
                                                 intentIdentifiers: [],
                                                 options: .customDismissAction)
    center.setNotificationCategories([generalCategory])
    let content = UNMutableNotificationContent()
    mvc.newNotification()
    let contentText = UserDefaults.standard.string(forKey: "contentText")
    content.title = NSString.localizedUserNotificationString(forKey: "The Better Life Challenge", arguments: nil)
    content.body = NSString.localizedUserNotificationString(forKey: "(contentText!)", arguments: nil)
    var dateInfo = DateComponents()
    dateInfo.hour = 4
    dateInfo.minute = 0
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: true)
    let request = UNNotificationRequest(identifier: "TBLC", content: content, trigger: trigger)
    center.add(request) { (error : Error?) in
        if let theError = error {
            print(theError.localizedDescription)
        }

感谢您的帮助!

您以称为didrecieveremoteNotification的方法进行此操作...当您收到新通知时,此方法会发射,而不是finishlaunchingingwithoptions ..

最新更新