O push silencioso rápido do iOS11 (busca em segundo plano, didReceiveRemoteNotification) não está mais funcionando
Eu esperava que o lançamento do iOS11 corrija o problema do envio silencioso, que estava na versão beta mais recente e na versão GM do iOS.
Atualmente, estou lutando para entender por que não recebo mensagens push silenciosas, o que realmente deve ativar meu aplicativo para executar algumas tarefas necessárias em segundo plano.
No iOS 10, apenas uso o recurso de busca em segundo plano e implementei o 'código de ativação' no meu AppDelegate, como o código abaixo.
No iOS 11, o código de registro ainda está funcionando bem e meu back-end também está entregando a notificação por push aos servidores Apples DEV (sandbox) e também aos servidores PROD (release de produção). Infelizmente a funçãofunc application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
nunca é chamado pelas notificações push silenciosas.
Eu realmente perdi alguma coisa aqui para o 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 ....
}
}
}