這是我的註冊通知操作UNTextInputNotificationAction當應用程序被殺害或屏幕鎖定SWIFT不叫3
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()
}
}
在這裏,我符合協議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()
}
}
在前面的f我執行一個http請求來回復推送通知中的消息。當我在通知中回覆消息並點擊發送按鈕時,會發生真正的問題,有時會執行http請求,有時候不會。我已經在我的應用程序的功能中添加了背景模式,事實上,我正在成功接收來自api的通知,api正在發送值爲'content-available'的值。最後,證書.pem在服務器中正常工作,所以。我錯過了什麼?