我正在使用firebase云消息发送推送通知。收到推送通知后,我下载了一些其他内容,然后发出本地通知。这适用于前台但不适用于后台。
我在youtube上关注了此tutorial。 这是我的代码,
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound]) { (granted, error) in
if granted {
UNUserNotificationCenter.current().delegate = self
Messaging.messaging().delegate = self
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
FirebaseApp.configure()
}
}
}
func applicationDidEnterBackground(_ application: UIApplication) {
Messaging.messaging().shouldEstablishDirectChannel = false
}
func applicationDidBecomeActive(_ application: UIApplication) {
connectFCM()
}
func connectFCM() {
Messaging.messaging().shouldEstablishDirectChannel = true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
UIApplication.shared.beginBackgroundTask {
}
fireLocalNotification(data:userInfo)// where additional content is downloaded and a local notification is fired
}
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
token = fcmToken
sendTokenToServer(token: fcmToken)
connectFCM()
}