应用关闭时的CloudKit订阅

时间:2016-03-11 08:44:37

标签: ios swift apple-push-notifications cloudkit

我正在使用CloudKit在设备之间同步数据。一切看起来很好(数据已更改 - >带有记录ID的推送通知 - >在云和本地coredata db中获取/编辑/删除此记录)如果应用程序处于活动状态。但是当我按下“主页”按钮时它就不起作用了。 这是我的代码:

首次启动应用时创建订阅

def toImage(f):
    documentName = f[:-4]

    def imageList(imgObject):       
        #get list of generated images
        imagePrefix = "{}tmp/{}/{}".format(path,documentName,documentName)

        if len(img.sequence) > 1:   
            images = [ ("{}-{}.jpg".format(imagePrefix, x.index), documentName) for x in img.sequence]
        else:
            images = [("{}.jpg".format(imagePrefix), documentName)]
        return images

    #store images for each file in tmp directory
    with WandImage(filename=path + f, resolution=300) as img:
        #create tmp directory
        if not os.path.exists(path + "tmp/" +  documentName):
            os.makedirs(path + "tmp/" +  documentName)

        #save images in tmp directory
        img.format = 'jpeg'
        img.save(filename=path + "tmp/" +  documentName + '/' + documentName + '.jpg')  
        imageL =  imageList(img)
        return imageL


def doOcr(imageList):
    print(imageList[0][1])
    content = "\n\n***NEWPAGE***\n\n".join([pytesseract.image_to_string(Image.open(fullPath), lang='nld') for fullPath, documentName in imageList])
    with open(path + "/txt/" + imageList[0][1] + ".txt", "w") as text_file:
        text_file.write(content)

sc = SparkContext(appName="OCR")
pdfFiles = sc.parallelize([f for f in os.listdir(sys.argv[1]) if f.endswith(".pdf")])
text = pdfFiles.map(toImage).foreach(doOCr)

的AppDelegate

func addNewRecordsSub() {
    let subscription = CKSubscription(recordType: "UserRecords", predicate: predicate, options: [.FiresOnRecordCreation, .FiresOnRecordUpdate, .FiresOnRecordDeletion])
    let notificationInfo = CKNotificationInfo()
    notificationInfo.shouldBadge = false
    subscription.notificationInfo = notificationInfo
    let privateDatabase = container.privateCloudDatabase
    privateDatabase.saveSubscription(subscription) { subscription, error in
        if error != nil {
            print(error)
        }
        NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
            self.addNewRecordCategoriesSub()
        })
    }
}


func addNewRecordCategoriesSub() {
    let subscription = CKSubscription(recordType: "UserRecordsCategories", predicate: predicate, options: [.FiresOnRecordCreation, .FiresOnRecordUpdate, .FiresOnRecordDeletion])
    let notificationInfo = CKNotificationInfo()
    notificationInfo.shouldBadge = false
    subscription.notificationInfo = notificationInfo
    let privateDatabase = container.privateCloudDatabase
    privateDatabase.saveSubscription(subscription) { subscription, error in
        if error != nil {
            print(error)
        }
        NSOperationQueue.mainQueue().addOperationWithBlock({ () -> Void in
            self.performSegueWithIdentifier("goToRootController", sender: self)
        })
    }
}

更新 好的,我发现它https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html

  

aps词典还可以包含content-available属性。值为1的content-available属性允许远程通知充当静默通知。当无声通知到达时,iOS会在后台唤醒您的应用程序,以便您可以从服务器获取新数据或进行后台信息处理。用户不会被告知由静默通知产生的新信息或更改信息,但他们可以在下次打开您的应用时找到相关信息。

     

对于静默通知,请注意确保aps词典中没有警报,声音或徽章有效负载。如果您不遵循此指南,则错误配置的通知可能会受​​到限制,并且不会在后台传递给应用程序,而是显示给用户。

现在它的工作方式如下:按主页按钮 - >再次打开应用程序 - >获取新数据。

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    UINavigationBar.appearance().barStyle = .Black
    //Push
    let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert]
    let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
    application.registerUserNotificationSettings(pushNotificationSettings)
    application.registerForRemoteNotifications()
    if let options: NSDictionary = launchOptions {
        let remoteNotification = options.objectForKey(UIApplicationLaunchOptionsRemoteNotificationKey) as? NSDictionary
        if let notification = remoteNotification {
            self.application(application,
                didReceiveRemoteNotification: notification as! [NSObject : AnyObject])
        }
    }
    return true
}

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    print("DEVICE TOKEN = \(deviceToken)")
}


func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    print(error)
}

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
    let ckNotification = CKNotification(fromRemoteNotificationDictionary: userInfo as! [String : NSObject])
    if ckNotification.notificationType == .Query {
        let queryNotification = ckNotification as! CKQueryNotification
        let recordID = queryNotification.recordID!
    //Saving/Update/delete record
}

但我看到另一个问题。如果我关闭应用程序(双击主页并轻扫),我无法获取数据。是我还是iOS不支持它?

0 个答案:

没有答案