我想在收到静默推送通知时呈现一个视图并传递此
push
数据。
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
{
printLog("Did Receive Remote Notification")
printLog(userInfo)
do {
let jsonResult = try JSONSerialization.data(withJSONObject: userInfo)
let push = try JSONDecoder().decode(RequestPushModel.self, from: jsonResult)
let single = RequestBearerCustomer(type: "\(push.type)", transport_id: "\(push.transport_id)", shortened_origin_address: "\(push.shortened_origin_address)", origin_lat: "\(push.origin_lat)", origin_lng: "\(push.origin_lng)", time: "\(push.time)", price: "\(push.price)", expire_time: "\(push.expire_time)")
if Constants.requestBearerCustomer.count > 0 {
Constants.requestBearerCustomer.insert(single, at: Constants.requestBearerCustomer.count - 1)
} else {
Constants.requestBearerCustomer.insert(single, at: 0)
}
printLog(single)
printLog(Constants.requestBearerCustomer[0])
} catch {
printLog(error)
}
试试这个:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
printLog("Did Receive Remote Notification")
printLog(userInfo)
do {
let jsonResult = try JSONSerialization.data(withJSONObject: userInfo)
let push = try JSONDecoder().decode(RequestPushModel.self, from: jsonResult)
let single = RequestBearerCustomer(type: "\(push.type)", transport_id: "\(push.transport_id)", shortened_origin_address: "\(push.shortened_origin_address)", origin_lat: "\(push.origin_lat)", origin_lng: "\(push.origin_lng)", time: "\(push.time)", price: "\(push.price)", expire_time: "\(push.expire_time)")
if Constants.requestBearerCustomer.count > 0 {
Constants.requestBearerCustomer.insert(single, at: Constants.requestBearerCustomer.count - 1)
} else {
Constants.requestBearerCustomer.insert(single, at: 0)
}
printLog(single)
printLog(Constants.requestBearerCustomer[0])
// Inform the system about the success of the background fetch
completionHandler(.newData)
} catch let error {
printLog("Error decoding push notification: \(error)")
// Inform the system that there was an error during the background fetch
completionHandler(.failed)
}
}