gpt4 book ai didi

ios - 当应用程序被杀死或屏幕被锁定时,UNTextInputNotificationAction 不会被调用 swift 3

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:31:38 24 4
gpt4 key购买 nike

这就是我注册通知操作的方式

func registerForPushNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("Permission granted: \(granted)")

guard granted else { return }

let action = UNTextInputNotificationAction(identifier: replyActionIdentifier, title: "Answer message", options: [UNNotificationActionOptions.init(rawValue: 0)], textInputButtonTitle: "Send", textInputPlaceholder: "Type your message")

let newsCategory = UNNotificationCategory(identifier: categoryIdentifier,
actions: [action],
intentIdentifiers: [],
options: [])

UNUserNotificationCenter.current().setNotificationCategories([newsCategory])

self.getNotificationSettings()
}
}

这里我遵循协议(protocol) UNUserNotificationCenterDelegate 并处理通知操作

extension AppDelegate: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {

let userInfo = response.notification.request.content.userInfo

// check if there is information in push's payload
guard let custom = userInfo["data"] as? [String: AnyObject] else {
completionHandler()
return
}

switch response.actionIdentifier {
case replyActionIdentifier:

if let message = response as? UNTextInputNotificationResponse {
if message.userText.isEmpty { return }

// encrypt the message
let publicKey = try! PublicKey(pemEncoded: Constantes.PUSH_KEY)
let clear = try! ClearMessage(string: message.userText, using: .utf8)
let encrypted = try! clear.encrypted(with: publicKey, padding: .PKCS1)
let messageUser = encrypted.base64String

// data inside the push
guard let user_id = custom["me"] as? String, let room_id = custom["room_id"] as? String else {
completionHandler()
return
}

// secutiry elements
guard let token = String(data: Keychain.load("push_token")!, encoding: .utf8),
let uuid = String(data: Keychain.load("UUID")!, encoding: .utf8) else {
completionHandler()
return
}

let key = Constantes.KEY
let chats = Chats()

// this executes an http request
chats.sendMessage(token: token, uuid: uuid, key: key!, message: messageUser, user_id: user_id, room_id: room_id)
}

default: break
}
completionHandler()

}

在之前的函数中,我执行了一个 http 请求来回复推送通知中的消息。真正的问题发生在我响应通知中的消息并点击“发送”按钮时,有时会执行 http 请求,有时不会。我已经在我的应用程序的功能中添加了后台模式,事实上我从 api 成功接收到通知,api 在 1 中发送值“content-available”。最后,证书 .pem 在服务器中正常工作,所以。我错过了什么?

最佳答案

我只是遇到了这个问题,不知道为什么我的快速回复在应用程序处于后台时有效,但在它被杀死时却无效。然后我开始通过源代码寻找一些东西,并在 UNNotificationCenter 中偶然发现了这个。 :

// The method will be called on the delegate when the user responded to the notification by opening the application, dismissing the notification or choosing a UNNotificationAction. The delegate must be set before the application returns from application:didFinishLaunchingWithOptions:.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler

如果您没看错评论的最后一部分,它说必须在从 application:didFinishLaunchingWithOptions: 返回之前设置委托(delegate)

这就是我的错误所在。我在完成 UNUserNotificationCenter.current().getNotificationSettings 时设置委托(delegate)

因此,您所要做的就是确保像这样设置委托(delegate) UNUserNotificationCenter.current().delegate = self之前 application:didFinishLaunchingWithOptions:返回

关于ios - 当应用程序被杀死或屏幕被锁定时,UNTextInputNotificationAction 不会被调用 swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46427199/

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