2016-09-30 5 views
5

알림에서 작업을 구현하려고합니다. 그리고 지금까지는 적절한 위임 함수를 트리거 할 수 있지만 앱을 두드린 후에는 포 그라운드로 가져 오지 않습니다.iOS10 : 로컬 알림에서 작업을 실행해도 앱이 포 그라운드로 이동하지 않습니다.

관련 코드 :

@available(iOS 10.0, *) 
func registerCategory() -> Void{ 
    print("register category") 
    let callNow = UNNotificationAction(identifier: "call", title: "Call now", options: []) 
    let clear = UNNotificationAction(identifier: "clear", title: "Clear", options: []) 
    let category : UNNotificationCategory = UNNotificationCategory.init(identifier: "IDENT123", actions: [callNow, clear], intentIdentifiers: [], options: []) 

    let center = UNUserNotificationCenter.currentNotificationCenter() 
    center.setNotificationCategories([category]) 
} 

@available(iOS 10.0, *) 
func scheduleNotification(event : String, interval: NSTimeInterval) { 

    print("schedule ", event) 

    let content = UNMutableNotificationContent() 

    content.title = event 
    content.body = "body" 
    content.categoryIdentifier = "CALLINNOTIFICATION" 
    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval, repeats: false) 
    let identifier = "id_"+event 
    let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger) 

    let center = UNUserNotificationCenter.currentNotificationCenter() 
    center.addNotificationRequest(request) { (error) in 

    } 
} 

@available(iOS 10.0, *) 
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) { 

    print("willPresent") 
    completionHandler([.Badge, .Alert, .Sound]) 
} 

@available(iOS 10.0, *) 
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler:() -> Void) { 

    let notification: UNNotification = response.notification 
    let UUID = notification.request.content.userInfo["UUID"] as! String 

    switch (response.actionIdentifier) { 
    case "COMPLETE": 

     UNUserNotificationCenter.currentNotificationCenter().removeDeliveredNotificationsWithIdentifiers([UUID]) 

    case "CALLIN": 

     let call = Call() 

     CalendarController.sharedInstance.fetchMeetingByUUID(UUID, completion: { (thisMeeting) -> Void in 
      if(!CallIn.Yield(thisMeeting).ConferenceCallNumber.containsString("None")){ 
       call._call(thisMeeting) 
      }else{ 
       //will open detail view, in case that no number were detected 
       NSNotificationCenter.defaultCenter().postNotificationName("OpenDetailViewOfMeeting", object: self, userInfo: ["UUID":UUID]) 
      } 
     }) 
     UNUserNotificationCenter.currentNotificationCenter().removeDeliveredNotificationsWithIdentifiers([UUID]) 
    default: // switch statements must be exhaustive - this condition should never be met 
     log.error("Error: unexpected notification action identifier: \(UUID)") 
    } 

    completionHandler() 
} 

내가 중단 점에 위임 기능 didReceiveNotificationResponse()를 칠 수 있어요,하고 예상되는 방법으로 내가 거기에 넣어 몇 가지 작업을 수행 있지만 (이있다 장치 호출을 시작하는 대신 알림 목록을 닫고 아무 일도 일어나지 않습니다. 그러나 수동으로 다시 열면 알리미 응용 프로그램을 열 수있는 권한이없는 것처럼 호출이 시작됩니다.

답변

4

나는 그 자신을 발견 했으므로 미래에 누군가에게 도움이 될 것입니다. 대답은 아주 단순한 것으로 판명되었습니다. 알림 작업을 만들 때 options이라는 매개 변수가 있습니다. 카테고리를 등록 할 때는 다음 두 가지 중 하나를 사용해야합니다 .Foreground 또는 .Destructive this :

func reisterCategory() { 
    let callNow = UNNotificationAction(identifier: NotificationActions.callNow.rawValue, title: "Call now", options: UNNotificationActionOptions.Foreground) 
    let clear = UNNotificationAction(identifier: NotificationActions.clear.rawValue, title: "Clear", options: UNNotificationActionOptions.Destructive) 
    let category = UNNotificationCategory.init(identifier: "NOTIFICATION", actions: [callNow, clear], intentIdentifiers: [], options: []) 
    let center = UNUserNotificationCenter.currentNotificationCenter() 
    center.setNotificationCategories([category]) 
} 
관련 문제