I was hoping that the iOS11 release will fix the silent push issue, which was in the latest betas and GM version of iOS.
Currently I'm struggling to understand, why I don't receive any silent push messages, which should actually wake up my app to perform some needed tasks in the background.
In iOS 10 I just use the background fetch capability and implemented the 'wake-up-code' in my AppDelegate like the code below.
In iOS 11 the registering code is still working fine and my backend is also delivering the push notification to Apples DEV servers (sandbox) and also to the PROD servers (production release). Unfortunately the function func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
is never called by the silent push notifications.
Did I actually miss something here for iOS 11?
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
// .. some variables here ...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// register silent push notification in backend
application.registerForRemoteNotifications()
// ... some code here ...
// Set Background Fetch Intervall for background services / terminated app
UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
// ... some code here ...
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data -> String in
return String(format: "%02.2hhx", data)
}
let token = tokenParts.joined()
logger.log.debug("Device Token: \(token)")
let realm = RealmController()
let user = realm.getLoggedInUserObject()
// Send push token to server
if let user = user {
let email = user.email!
let serverController = ServerController.serverController
serverController.sendPushToken(token: token, email: email) { status in
if status == 201 {
// ... some code here ...
} else {
// ... some code here ...
}
}
}
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
logger.log.debug(error)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
logger.log.debug(userInfo)
let aps = userInfo["aps"] as! [String: AnyObject]
if aps["content-available"] as? Int == 1 {
// .... some silent push tasks here ....
}
}
}