I'm trying to use silent notifications with FCM to trigger my iOS app to download new data from the database. I've defined my payload and options as such:
var payload = {
notification: {
title: "DataSync",
body: "DataSync"
}
};
var options = {
contentAvailable: true,
priority: "high"
};
I also have the following code in my didFinishLaunchingWithOptions
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(
options: authOptions,
completionHandler: {_, _ in })
FIRMessaging.messaging().remoteMessageDelegate = self
} else {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(settings)
}
I need to make sure that data downloading works even when the app is in the background or closed. In order to do that, I need to trigger performFetchWithCompletionHandler from didReceiveRemoteNotification. However, only applicationReceivedRemoteMessage is being called right now (which only gets called when app is in foreground) even though I've defined my payload to be a notification, not a data message.
Update: Additional Info
When I use NWPusher to send a notification directly through APNs, if the app is in the foreground, the following method from UNUserNotificationCenterDelegate is called.
userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
And if the app is in the background, the following method is called:
application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
However, these methods are not called when sending a silent notification through FCM admin sdk. Only applicationReceivedRemoteMessage is called.
My question is: 1. How do I get didReceiveRemoteNotification to be called so I that I can download data? 2. How should I write my code in performFetchWithCompletionHandler to retrieve information from the Firebase database and ensure that the data has finished downloading? Is it just a regular Firebase query?