【问题标题】:Apple Push Notification Action ButtonsApple 推送通知操作按钮
【发布时间】:2017-09-18 15:54:20
【问题描述】:

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

【问题讨论】:

  • 你的“身体”动作是什么?我在上面的代码中看不到它。我只看到接受和拒绝。
  • 触发body动作的代码在didReceive response中。解析代码在 SWReveal 中执行一个函数,当身体被点击时它几乎可以工作。如果用户点击操作按钮,我想禁用该行为。

标签: ios swift push-notification apple-push-notifications


【解决方案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

}

这样,如果动作标识符与任何一个动作匹配,即动作按钮已被按下,那么函数的其余部分将不会被执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2023-03-10
    • 2016-08-18
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    相关资源
    最近更新 更多