0

如果我發送遠程推送通知,則在後臺/前臺中一切正常。但是,如果我殺了我的應用程序,並想要處理推送通知,應用程序啓動並在幾ms後崩潰。應用程序未運行時的遠程推送通知崩潰

這裏是我的didFinishLaunchingWithOptions功能:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 
     UNUserNotificationCenter.current().requestAuthorization(options: 
      [[.alert, .sound, .badge]], 
      completionHandler: { (granted, error) in 
      // Handle Error 
     }) 
     application.registerForRemoteNotifications() 

     if let options: NSDictionary = launchOptions as NSDictionary? { 
      let remoteNotification = 
       options[UIApplicationLaunchOptionsKey.remoteNotification] 

      if let notification = remoteNotification { 
       self.application(application, didReceiveRemoteNotification: 
        notification as! [AnyHashable : Any], 
           fetchCompletionHandler: { (result) in 
       }) 

      } 
     } 

     return true 
} 

而且我didReceiveRemoteNotification功能

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { 

     let tabBar: UITabBarController = self.window?.rootViewController as! UITabBarController 
     let navController: UINavigationController = tabBar.viewControllers![1] as! UINavigationController 
     let viewController = navController.viewControllers[0] as! CompanyProfileTableViewController 
     //let viewController = tabBar.viewControllers![1] as! CompanyProfileTableViewController 

     let notification: CKNotification = CKNotification(fromRemoteNotificationDictionary: 
       userInfo as! [String : NSObject]) 

     print("\(notification)") 

     if (notification.subscriptionID == "company-changed") { 
      let queryNotification = 
       notification as! CKQueryNotification 
      print("\(queryNotification)") 

      let recordID = queryNotification.recordID 

      viewController.fetchRecord(recordID!) 
     } 

    } 

I'm處理這一問題的足夠多小時,但不容找到一個妥善的解決辦法。我也檢查了

Handling Push Notifications when App is NOT running

如果我重新啓動應用程序,由rPN引起的標籤變化。由於我必須殺死應用程序,因此我無法在rPN發送到殺死的應用程序時調試Xcode中的代碼。

下面是函數獲取記錄和更新標籤

//Fetch records from DB 
func fetchRecord(_ recordID: CKRecordID) -> Void { 
    let privateDatabase = CKContainer.default().privateCloudDatabase 
    var recordIDArray: [CKRecordID] = [] 
    recordIDArray.append(recordID) 

    let fetchRecordsWithID = CKFetchRecordsOperation(recordIDs: recordIDArray) 
    fetchRecordsWithID.fetchRecordsCompletionBlock = { (records, error) in 
     if error != nil { 
      print("Error") 
     } else { 
      DispatchQueue.main.async() { 
       self.currentRecord = records?[recordID] 

       let data = NSKeyedArchiver.archivedData(withRootObject: self.currentRecord ?? "none") 
       UserDefaults.standard.set(data, forKey: "companyRecord") 
       UserDefaults.standard.synchronize() 

       if let retrivedData = UserDefaults.standard.object(forKey: "companyRecord") { 
        let companyRecord = NSKeyedUnarchiver.unarchiveObject(with: retrivedData as! Data) as! CKRecord 
        self.companyNameLabel.text = companyRecord["companyName"] as? String 
        self.companyStreetLabel.text = companyRecord["companyStreet"] as? String 
        self.companyZipCodeLabel.text = companyRecord["companyZipCode"] as? String 
       } 

      } 
     } 
    } 
    privateDatabase.add(fetchRecordsWithID)   
} 

跟蹤堆棧:

Trace Stack

我想這個問題是在取錄功能。

錯誤:

; partial apply forwarder for Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt, flags : Swift.UInt32) -> Swift.Never).(closure #2) 
    0x1003d11e4 <+96>: nop  
    0x1003d11e8 <+100>: mov x0, x25 
    0x1003d11ec <+104>: mov x1, x24 
    0x1003d11f0 <+108>: mov x2, x23 
    0x1003d11f4 <+112>: mov x4, x8 
    0x1003d11f8 <+116>: bl  0x1002c4b80    ; function signature specialization <preserving fragile attribute, Arg[1] = [Closure Propagated : reabstraction thunk helper from @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) ->() to @callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) -> (@out()), Argument Types : [@callee_owned (@unowned Swift.UnsafeBufferPointer<Swift.UInt8>) ->()]> of generic specialization <preserving fragile attribute,()> of Swift.StaticString.withUTF8Buffer <A> ((Swift.UnsafeBufferPointer<Swift.UInt8>) -> A) -> A 
-> 0x1003d11fc <+120>: brk #0x1 
+0

你能告訴我們什麼是你的堆棧跟蹤應用程序崩潰時? – carmine

+0

@carmine你的意思是堆棧跟蹤? – Luca

+0

http://stackoverflow.com/a/26341275/1622430和以下.. – carmine

回答

0

我終於找到了解決辦法。問題是我的fetchRecord函數。使用「殺死」應用執行rPN時,標籤未被初始化。

因此,有解決這個問題的方法有兩種:

1:

修改didReceiveRemoteNotification方法:

if (notification.subscriptionID == "company-changed") { 
      let queryNotification = 
       notification as! CKQueryNotification 
      print("\(queryNotification)") 

      let recordID = queryNotification.recordID 
      tabBar.selectedIndex = 1 
      viewController.fetchRecord(recordID!) 
} 

我補充這一點 - >

tabBar.selectedIndex = 1 

這將顯示viewController和標籤初始化罰款。

另一種解決方法是將部分標籤設置爲viewWillAppear或viewDidLoad。

相關問題