我尝试在FCM中使用静默通知来触发我的iOS应用从数据库下载新数据。我已经定义了我的有效负载和选项:
var payload = {
notification: {
title: "DataSync",
body: "DataSync"
}
};
var options = {
contentAvailable: true,
priority: "high"
};
我的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)
}
我需要确保即使应用程序处于后台或关闭时数据下载仍然有效。为了做到这一点,我需要从didReceiveRemoteNotification触发performFetchWithCompletionHandler。但是,现在只调用applicationReceivedRemoteMessage(只有当app在前台时才会被调用),即使我已将有效负载定义为通知,而不是数据消息。
更新:其他信息
当我使用NWPusher直接通过APN发送通知时,如果应用程序位于前台,则会调用UNUserNotificationCenterDelegate中的以下方法。
userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
如果应用程序在后台,则会调用以下方法:
application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void)
但是,通过FCM admin sdk发送静默通知时,不会调用这些方法。只调用applicationReceivedRemoteMessage。
我的问题是: 1.如何调用didReceiveRemoteNotification以便我可以下载数据? 2.我应该如何在performFetchWithCompletionHandler中编写代码以从Firebase数据库中检索信息并确保数据已完成下载?它只是常规的Firebase查询吗?