2017-09-18 19 views
1

私のアプリケーションのためのリモート通知を設定し、それは次のように動作します。 ユーザーが通知の本文をタップすると、関数が呼び出されます(ユーザーが特定のViewControllerに移動します)。 しかし、ユーザがアクションボタンの1つをタップすると、ボタンのアクションはボディタップアクションと同じように実行されます。どのようにして、アクションボタンは、そのアクションだけを実行することなく、そのアクションを実行することができます。ここでAppleプッシュ通知アクションボタン

は私のサンプルコードです:

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

    completionHandler([.alert,.sound,.badge]) 
} 

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

    let userInfo = response.notification.request.content.userInfo as! [String:AnyHashable] 

    // parse userInfo and execute a function in SWReveal to show the appropriate ViewController 

    let action = response.actionIdentifier 

    if action == "acceptFriendRequest" { 
     print("Friend Request Accepted") 
    } 
} 

func setCategories() { 
    if #available(iOS 10.0, *) { 
     let acceptFriendRequest = UNNotificationAction(
      identifier: "acceptFriendRequest", 
      title: "Accept", 
      options: []) 
     let rejectFriendRequest = UNNotificationAction(identifier: "rejectFriendRequest", title: "Reject", options: [.destructive]) 
     let FrReq = UNNotificationCategory(
      identifier: "FrReq", 
      actions: [acceptFriendRequest, rejectFriendRequest], 
      intentIdentifiers: [], 
      options: [.customDismissAction])} 
+0

"body"アクションとは何ですか?私は上記のコードでそれを見ることはできません。私は受け入れると拒否を参照してください。 – matiastofteby

+0

ボディアクションをトリガーするコードは 'didReceive response'です。解析コードは、本体がタップされたときに動作するSWRevealの関数を実行します。ユーザーがアクションボタンをタップすると、その動作を無効にします。 – Tarek

答えて

1

私は完全にそれがデリゲートメソッドだとして呼び出されるメソッドを回避する方法はないと思います。しかし、関数の先頭で、簡単なガード文が同じトリックを行う必要があります:機能の

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

    guard response.actionIdentifier != "acceptFriendRequest" && response.actionIdentifier != "rejectFriendRequest" else { return } 

    let userInfo = response.notification.request.content.userInfo as! [String:AnyHashable] 

    // parse userInfo and execute a function in SWReveal to show the appropriate ViewController 

} 

アクション識別子は、アクションのいずれかがアクションボタンが押されている、すなわち、一致した場合は、この方法は、その後、残りの部分実行されません。

関連する問題