Преглед на файлове

call completion handler on didReceiveRemoteNotification

B. Petersen преди 4 години
родител
ревизия
34f12a8827
променени са 1 файла, в които са добавени 17 реда и са изтрити 3 реда
  1. 17 3
      deltachat-ios/AppDelegate.swift

+ 17 - 3
deltachat-ios/AppDelegate.swift

@@ -362,18 +362,32 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
 
     // `didReceiveRemoteNotification` is called by iOS when a push notification is received.
     //
-    // we need to ensure IO is running as the functionb may be called from suspended state
+    // we need to ensure IO is running as the function may be called from suspended state
     // (with app in memory, but gracefully shut down before; sort of freezed).
     // if the function was not called from suspended state,
-    // the call to maybeStartIo() did nothing, therefore, interrupt and force fetch
+    // the call to maybeStartIo() did nothing, therefore, interrupt and force fetch.
+    //
+    // we have max. 30 seconds time for our job and to call the completion handler.
+    // as the system tracks the elapsed time, power usage, and data costs, we return faster,
+    // after 10 seconds, things should be done.
+    // (see https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application)
+    // (at some point it would be nice if we get a clear signal from the core)
     func application(
         _ application: UIApplication,
-        didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
+        didReceiveRemoteNotification userInfo: [AnyHashable: Any],
+        fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
         logger.verbose("Notifications: didReceiveRemoteNotification \(userInfo)")
         increaseDebugCounter("notify-remote-receive")
 
         dcContext.maybeStartIo()
         dcContext.maybeNetwork()
+
+        DispatchQueue.main.asyncAfter(deadline: .now() + 10) {
+            if !self.appIsInForeground {
+                self.dcContext.stopIo()
+            }
+            completionHandler(.newData)
+        }
     }
 
     private func increaseDebugCounter(_ name: String) {