2016-09-30 7 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

私は自分自身の理由を知ったので、これは将来誰かに役立つかもしれません。答えは非常に簡単であることが判明しました。通知のアクションを作成するときは、このパラメーターがあります:オプション。カテゴリを登録するときは、次のように.Foregroundまたは.Destructiveのどちらかにする必要があります。

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]) 
} 
関連する問題