尝试从远程通知快速从 didReceive 响应内部运行函数 更新

问题描述

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

userNotificationCenter 响应的 didReceive 方法中,我需要能够调用我的 ApiService 将消息发送到我的节点服务器,该服务器通过管理 sdk 直接与 firebase 通信.问题是,每次我尝试从该函数内部调用一个方法时,我都会收到一个错误

表达类型不明确,没有更多上下文

这是我的 didReceive 方法,为了简单起见在这文章中进行了简化...

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 类中添加一个私有方法然后调用方法来更改它,但我仍然遇到相同的错误。我不明白为什么它认为表达式是模棱两可的,除了我构建的类之外,我的项目中没有其他 ApiService 类。 我这样做完全错误吗?我只是希望能够将用户通知中键入的消息发送回服务器,以便将其添加到聊天中。提前感谢您的任何帮助,这个让我挠头,掉了一些头发。

解决方法

这里面有很多错别字 - 有些是允许的,有些会造成像您所面临的问题。

/// 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,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
        }
    }
}