2017-04-15 35 views
0

自从我的应用程序出现问题以后,显然每次应用程序启动时都会调用新的通知函数(在AppDelegate中)。如何让我的newNotification()-函数仅用于新通知而不用于应用程序开始启动?AppDelegate:只有在通知函数时才会调用函数,如何?

下面是从的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) 
     } 

感谢您的帮助!

回答

1

您可以在称为didRecieveRemoteNotification的方法中执行此操作...当您收到新通知时触发此方法,而不是didFinishLaunchingWithOptions。

相关问题