gpt4 book ai didi

ios - UNNotificationAction 未显示在远程通知中

转载 作者:可可西里 更新时间:2023-11-01 01:19:52 25 4
gpt4 key购买 nike

我正在实现可操作的远程通知,但是在 notification 到来时没有显示操作按钮。

负载

{"aps":{"alert":"Testing.. (11)","badge":1,"sound":"default"},"category":"newnote"}

我的代码

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

self.configureUserNotifications()
self.registerForRemoteNotification()

return true
}


func registerForRemoteNotification() {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge]) { (granted, error) in
if error == nil{
UIApplication.shared.registerForRemoteNotifications()
}
}


func configureUserNotifications() {
let acceptAction = UNNotificationAction(identifier:
"accept", title: "✅ Accept note", options: [])
let rejectAction = UNNotificationAction(identifier:
"reject", title: "❌ Reject note", options: [])
let category =
UNNotificationCategory(identifier: "newnote",
actions: [acceptAction,rejectAction],
intentIdentifiers: [],
options: [])

UNUserNotificationCenter.current()
.setNotificationCategories([category])
}



func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Registration for remote notifications failed")
print(error.localizedDescription)
}

func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("Registered with device token: \(deviceToken.hexString)")
self.deviceToken = deviceToken.hexString
}


//Called when a notification is delivered to a foreground app.

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("User Info = ",notification.request.content.userInfo)
completionHandler([.alert, .badge, .sound])
}

//Called to let your app know which action was selected by the user for a given notification.

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("User Info = ",response.notification.request.content.userInfo)
print("Response received for \(response.actionIdentifier)")

if response.actionIdentifier == "accept" {

}
else if response.actionIdentifier == "reject" {

}
completionHandler()
}

最佳答案

我想如果你移动 UNUserNotificationCenter.current()
.setNotificationCategories([category])
到你的 registerForRemoteNotification 函数,问题就会解决。像这样:

 if #available(iOS 8.0, *) {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: Set([category]))
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}

关于ios - UNNotificationAction 未显示在远程通知中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44381750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com