2017-04-15 34 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。

相關問題