苹果推送通知操作按钮

问题描述:

我为我的应用程序设置了远程通知,它的工作原理如下。 如果用户点击通知的正文,它会调用一个函数(将用户带到特定的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

我不认为有被称为,因为它是一个委托方法完全避免的方法的一种方式。然而,在函数的开始一个简单的后卫声明应该做同样的伎俩:

@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 

} 

如果动作标识符匹配任何的行动,即在操作按钮被按下这样,那么该功能的其余部分将不会执行。