尝试从远程通知中快速从 didReceive 响应中运行函数
Posted
技术标签:
【中文标题】尝试从远程通知中快速从 didReceive 响应中运行函数【英文标题】:Attempting to run a function from inside the didReceive response in swift from a remote notification 【发布时间】:2021-06-24 18:56:16 【问题描述】:所以我有一个设置,我正在尝试学习如何使用 firebase 和 swift 制作一个基本的聊天应用程序。我已经设置了所有通知类别,我可以获取通知,并且可以单击我设置的操作。我开始升级它以允许在通知操作中“回复”,这就是我遇到问题的地方。
在 didReceive
响应的 userNotificationCenter
方法中,我需要能够调用 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
类。
我这样做完全错了吗?我只是希望能够将用户在通知中键入的消息发送回服务器,以便可以将其添加到聊天中。提前感谢您的任何帮助,这个让我挠头,掉头发。
【问题讨论】:
尝试将每个参数的类型强制为预期类型。 您的函数类型不同,我认为它是字符串或 int 并且 userInfo["to"] 返回任何类型,因此您需要转换值 【参考方案1】:这里有很多错别字 - 有些是允许的,有些会造成您所面临的问题。
/// 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
【讨论】:
以上是关于尝试从远程通知中快速从 didReceive 响应中运行函数的主要内容,如果未能解决你的问题,请参考以下文章
应用程序未运行时未调用本地通知的 UNUserNotificationCenterDelegate didReceive 响应
在 NotificationService 中禁用从 didReceive 接收推送
WatchOS 动态通知不适用于 Xcode 11.1 及更高版本 - 未调用 didReceive