【问题标题】:Interactive push notification action button click not working交互式推送通知操作按钮单击不起作用
【发布时间】:2018-08-20 05:41:36
【问题描述】:

我是第一次处理交互式推送通知。我遇到了单击操作按钮的问题。我已经使用代码创建了推送通知。

          let acceptAction = UIMutableUserNotificationAction()
            acceptAction.identifier = "ACCEPT_REQUEST"
            acceptAction.title = "Accept"
            acceptAction.activationMode = .background
            acceptAction.isAuthenticationRequired = true
            acceptAction.isDestructive = false

            // Reject Action
            let rejectAction = UIMutableUserNotificationAction()
            rejectAction.identifier = "REJECT_REQUEST"
            rejectAction.title = "Reject"
            rejectAction.activationMode = .background
            rejectAction.isAuthenticationRequired = true
            rejectAction.isDestructive = false

            let requestCategory = UIMutableUserNotificationCategory()
            requestCategory.identifier = "CONNECTION_REQUEST"

            let categoriesForSettings = NSSet(array: [requestCategory,clubRequestCategory,groupRequestCategory,referralCategory])//NSSet(objects: requestCategory)

            // Register the notification settings.
            let types:UIUserNotificationType = [.alert, .badge, .sound]
            let newNotificationSettings = 
 UIUserNotificationSettings(types: types, categories: categoriesForSettings as! Set<UIUserNotificationCategory>)

UIApplication.shared.registerUserNotificationSettings(newNotificationSettings)

这是我为处理推送操作编写的代码。

    func application(_ application: UIApplication, handleActionWithIdentifier identifier: String?, forRemoteNotification userInfo: [AnyHashable : Any], completionHandler: @escaping () -> Void) {

if(identifier == "ACCEPT_REQUEST" || identifier == "REJECT_REQUEST")
    {
        // API Call goes here 
    }

}

我尝试调试代码,我看到用 API 函数编写的日志已打印,但它没有到达服务器。如果我从后台 API 调用成功启动应用程序并且还从服务器获得响应。我将 Alamofire 用于 API 相关的东西。

谁能帮我摆脱困境?

如果有人需要更多详细信息,请告诉我。

【问题讨论】:

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


    【解决方案1】:

    您正在使用已弃用的方法。请查看UNNotificationAction 操作。

    private let categoryIdentifier = "AcceptOrReject"
    
    private enum ActionIdentifier: String {
      case Accept, Reject
    }
    
    private func registerCustomActions() {
      let accept = UNNotificationAction(identifier: ActionIdentifier.Accept.rawValue,
                                        title: "Accept")
      let reject = UNNotificationAction(identifier: ActionIdentifier.Reject.rawValue,
                                        title: "Reject")
    
      let category = UNNotificationCategory(identifier: categoryIdentifier, actions: [accept, reject],
                                            intentIdentifiers: [])
      UNUserNotificationCenter.current().setNotificationCategories([category])
    }
    

    【讨论】:

    • 我已经尝试过了,但对我不起作用。仍然 API 调用无法到达服务器。
    • 我的应用程序中出现了同样的问题,API 响应在单击的操作按钮上不起作用。你找到解决办法了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 2012-06-16
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多