0

リモートプッシュ通知を送信すると、すべてがバックグラウンド/フォアグラウンドで良好に機能します。しかし、私は私のアプリを殺すとプッシュ通知を処理したい場合は、アプリケーションが起動し、数ミリ秒後にクラッシュします。アプリケーションが実行されていないときのリモートプッシュ通知

は、ここに私の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!) 
     } 

    } 

- 私が、適切な解決策を見つけるcan't。私もチェックしました

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を実行すると、ラベルが初期化されませんでした。

第一: - >

tabBar.selectedIndex = 1 

この意志

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

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

私はこれを追加します。

didReceiveRemoteNotification方法を変更し

だからこの問題を解決するには2つの方法がありますviewControllerを表示し、ラベルは正常に初期化されます。

他の解決策は、新しい値を持つラベルをviewWillAppearまたはviewDidLoadに設定することでした。

関連する問題