SWIFT - 从 userInfo 获取值以填充推送通知的字段
SWIFT - Get values from userInfo to populate a fields of a Push Notification
我需要使用 userInfo 填充推送通知(.title
和 .body
)的字段。我什么都试过了,但我只能输入 "date" 代码块。我能怎么做?
谢谢。
这是我从控制台发送推送通知时的输出 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)
}
在您的 userInfo 中您已经有一个字典!
您可以这样设置您的标题和留言
let title = userInfo["title"]
let message = userInfo["message"]
我需要使用 userInfo 填充推送通知(.title
和 .body
)的字段。我什么都试过了,但我只能输入 "date" 代码块。我能怎么做?
谢谢。
这是我从控制台发送推送通知时的输出 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)
}
在您的 userInfo 中您已经有一个字典! 您可以这样设置您的标题和留言
let title = userInfo["title"]
let message = userInfo["message"]