2017-09-18 175 views
1

我爲我的應用程序設置了遠程通知,它的工作原理如下。 如果用戶點擊通知的正文,它會調用一個函數(將用戶帶到特定的ViewController)。 但是,當用戶點擊其中一個動作按鈕時,該按鈕的動作將被執行以及身體敲擊動作。我如何才能讓動作按鈕執行其動作,而不需要執行其動作。蘋果推送通知操作按鈕

這裏是我的代碼示例:

@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

你的「身體」動作是什麼?我在上面的代碼中看不到它。我只看到接受和拒絕。 – matiastofteby

+0

觸發主體操作的代碼位於'didReceive響應'中。解析代碼在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 

} 

如果動作標識符匹配任何的行動,即在操作按鈕被按下這樣,那麼該功能的其餘部分將不會執行。