尝试 运行 来自远程通知的 swift 中 didReceive 响应中的函数

Attempting to run a function from inside the didReceive response in swift from a remote notification

所以我有一个设置,我正在尝试学习如何使用 firebase 和 swift 制作一个基本的聊天应用程序。我已经设置了所有通知类别,我可以获得通知,并且可以单击我设置的操作。我开始升级它以允许在通知操作中进行“回复”,这就是我 运行 遇到问题的地方。

在我的 userNotificationCenter 方法中 didReceive 响应,我需要能够调用我的 ApiService 将消息发送到我的节点服务器,该节点服务器通过管理SDK。问题是,每次我尝试从该函数内部调用方法时,我都会收到错误消息:

Type of expression is ambiguous without more context

这是我的 didReceive 方法,为了简单起见在 post...

中进行了简化
func userNotificaitonCenter(_ center: UNUsernotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  let userInfor = response.notificaiton.request.content.userInfo
  switch response.actionIdentifier {
    case Notifications.Actions.reply:
      let textResponse = response as! UNTextInputNotificationResponse
      let userText = textResponse.userText
      let messageFrom = userInfo["from"]
      let myID = userInfo["to"]
      ApiService.sendNewMessage(myId, to: messageFrom, message: userText) { (_) in } <-This line gives the error
      break
    ...
 }
}

我已经尝试通过在我的 AppDelegate class 中添加私有方法然后调用该方法来更改它,但我仍然遇到相同的错误。我不明白为什么它认为表达式有歧义,我的项目中除了我构建的那个之外没有其他ApiService class。 我这样做完全错了吗?我只想能够将用户在通知中键入的消息发送回服务器,以便将其添加到聊天中。预先感谢您的任何帮助,这个让我抓狂并掉了一些头发。

这里有很多拼写错误 - 有些是允许的,有些会造成您所面临的问题。

/// 1. `userNotificaitonCenter` - typo in Notification - allowed (you may be able to compile with it, SDK won't be able provide you this callback
/// 2. `center: UNUsernotificationCenter` - small n in notification (invalid type name - not allowed to compile - which you are seeing the compiler complaining about in it's weirdest form)
/// 3. `response.notificaiton.request.content.userInfo` - typo in `notification` (not allowed to compile)
/// 4. `let userInfor = ...` vs `userInfo["from"]` - compiler has no idea what `userInfo` is - it only knows about `userInfor` 
/// 5. `let userInfor = ...` vs `userInfo["to"]` - compiler has no idea what `userInfo` is - it only knows about `userInfor` 
/// 6. `ApiService.sendNewMessage` - Is it a class/static method or instance method? Do you want to do something like ApiService.shared.sendNewMessage ???
/// 7. `ApiService.sendNewMessage(myId, to: messageFrom` Does it expect String in both of these parameters? OR Int in first & String in second parameter?
func userNotificaitonCenter(_ center: UNUsernotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
  let userInfor = response.notificaiton.request.content.userInfo
  switch response.actionIdentifier {
    case Notifications.Actions.reply:
      let textResponse = response as! UNTextInputNotificationResponse
      let userText = textResponse.userText
      let messageFrom = userInfo["from"]
      let myID = userInfo["to"]
      ApiService.sendNewMessage(myId, to: messageFrom, message: userText) { (_) in } <-This line gives the error
      break
    ...
 }
}

更新

它应该是这样的(拼写错误已修复,添加了一些虚拟 ApiService 代码以使其编译)

import Foundation
import UserNotifications

class ApiService {
    static let shared = ApiService()
    func sendNewMessage(to toID: Int, from fromID: Int, message: String) {}
}

class Test: NSObject, UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        switch response.actionIdentifier {
        case "reply":
            let textResponse = response as! UNTextInputNotificationResponse
            let userText = textResponse.userText
            let from = userInfo["from"] as? Int ?? 0
            let to = userInfo["to"] as? Int ?? 0
            ApiService.shared.sendNewMessage(to: to, from: from, message: userText)
            break
        default: break
        }
    }
}