【发布时间】:2018-11-30 14:15:41
【问题描述】:
我需要使用 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!)
}
【问题讨论】:
-
请在控制台中分享您的打印数据,以便我们为您提供帮助..
标签: swift push-notification notifications apple-push-notifications push