我第一次处理交互式推送通知。我遇到了动作按钮点击的问题。我已经使用代码创建了推送通知。
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相关的事情。
任何人都可以帮我解决这个问题吗?
如果有人需要更多详情,请告诉我。
答案 0 :(得分:0)
您正在使用弃用的方法。请改为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])
}