gpt4 book ai didi

SWIFT - 从 userInfo 获取值以填充推送通知的字段

转载 作者:行者123 更新时间:2023-11-28 14:00:59 27 4
gpt4 key购买 nike

我需要使用 userInfo 填充推送通知的字段(.title.body)。我什么都试过了,但我只能输入“日期”代码块。我能怎么做?谢谢。

这是我从控制台 php 发送推送通知时的输出: OUTPUT

我在 AppDelegate 中的代码:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
//OTHER CODE
let state = UIApplication.shared.applicationState
if state == .background || state == .inactive {
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
let data = userInfo["data"] as! NSString //If i try to cast to NSDictionary, it will give me nil. Instead in this way i can see my output.
print("DATA: \n\(data)\n")
let title = ?? HELP
let message = ?? ME!
content.title = "\(title)"
content.body = "\(message)"
content.sound = .default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "t", content: content, trigger: trigger)
center.add(request, withCompletionHandler: nil)
} else if state == .active {
self.showAlertAppDelegate(title: "Okkey", message: "Test",imageName: "", buttonTitle: "OK", window: self.window!)
}

最佳答案

你可以试试

struct Root : Codable {
let title:String
let message:String
}

if let str = userInfo["data"] as? String {
let res = try? JSONDecoder().decode(Root.self,from:str.data(using:.utf8)!)
print(res?.title)
print(res?.message)
}

关于SWIFT - 从 userInfo 获取值以填充推送通知的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53559234/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com