尝试从远程通知中快速接收响应内部运行函数



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

在我的userNotificationCenterdidReceive响应方法中,我需要能够调用我的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, 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
}
}
}

最新更新