当应用程序处于后台并连接到Xcode时,一切正常,但是当我拔掉任何iOS设备并运行应用程序时,请移至后台并发送远程通知,didReceiveRemoteNotification:fetchCompletionHandler未被调用。
以下是代码段:
func application(_ application: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register: \(error)")
}
// MARK: - Notification Delegate
func application(_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if UserDefaults.standard.value(forKey: Constants.Key.authToken) != nil {
print("Payload is: \(userInfo)")
var dataDict : [String:Any] = [:]
if (userInfo["data"] != nil){
dataDict = userInfo["data"] as! [String : Any]
}
// ID : notification type
var idType = 0
if (dataDict["id"] != nil){
idType = dataDict["id"] as! Int
}
let state = UIApplication.shared.applicationState
print("state is \(state)")
// Application in forground state...
if state == .active {
self.postNotificationOfType(idType, dict: dataDict)
}
// Application in background state...
else if state == .background || state == .inactive {
self.postNotificationOfType(idType, dict: dataDict)
}
}
else{
print("User is logged out!")
}
completionHandler(.newData)
}
func postNotificationOfType(_ idType:Int, dict:[String:Any]){
// service request received
if idType == 1{
if (dict["serviceRequestId"] != nil){
NotificationCenter.default.post(name: Notification.Name("ServiceRequestReceived"), object: nil, userInfo: dict)
}
}
// service request canceled by client
else if idType == 2 {
NotificationCenter.default.post(name: Notification.Name("ServiceRequestCanceled"), object: nil)
}
// service started by client
else if idType == 5 {
NotificationCenter.default.post(name: Notification.Name("ServiceStartedByClient"), object: nil)
}
// service started by client
else if idType == 6 {
NotificationCenter.default.post(name: Notification.Name("ServiceEndedByClient"), object: nil)
}
}
这是我的有效载荷:
{
"aps" : {
"content-available" : 1
},
"data" : 1
}
任何帮助将不胜感激。 提前谢谢。