2017-09-26 131 views
1

这是我的注册通知操作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在服务器中正常工作,所以。我错过了什么?

回答

0

我只是有这个问题,并没有线索为什么我的快速回复工作,如果应用程序在后台,但不是当它被杀害。然后,我在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:

返回之前设置的评论的最后一部分

这是我的错误所在。我设置委托在UNUserNotificationCenter.current().getNotificationSettings

建成因此,所有你需要做的是确保你这样设置UNUserNotificationCenter.current().delegate = self委托application:didFinishLaunchingWithOptions:返回

相关问题