交互式推送通知操作按钮单击不起作用



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

          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相关的东西。

谁能帮我从中走出来?

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

您使用的是已弃用的方法。 请改为查看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])
}

最新更新