簡體   English   中英

關於遠程通知的后台任務在一段時間后暫停

[英]Background task on remote notification suspended after a short while

我正在使用帶有content-available:true標志的遠程通知來在后台啟動靜態推送通知並處理或從遠程API獲取更新。 當應用程序處於前台時,代碼執行正常,或者在上次運行后處於掛起狀態。

在后台測試期間,當系統基於傳入的靜默推送通知啟動應用程序時,代碼僅部分處理,並且應用程序在大約150毫秒后快速暫停。 我預計應用程序將有30秒的時間來處理傳入通知及其有效負載。 如果我需要更多時間處理和/或獲取新數據,是否需要調整應用程序功能或請求后台任務?

部署目標iOS 8,在iOS 9上測試.Xcode 7.3.1,Swift 2.2.1。

功能:后台模式開,模式:啟用遠程通知

AppDelegate中

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    if let userInfo = userInfo as? [String: AnyObject] {

        // Processing of the payload is done in separate Operation class (using Operations framework)
        // The completion handler is called on the end of the processing/fetching in that operation
        // But in case of launching the app in the background it never reaches the call of the completion handler

        let parseNotificationOperation = ParseNotificationOperation(userInfo: userInfo, fetchCompletionHandler: completionHandler)
        MainService.shared.enqueueApnsOperation(parseNotificationOperation)
    }
}

馬丁科爾斯,

您可以使用expirationHandlers來延長后台執行時間。 雖然iOS分配給您的應用程序需要多長時間取決於我們無法控制的各種因素,但我們注意到它主要提供我們的應用程序在后台執行3分鍾。

這是你如何實現它:)

你在AppDelegate聲明,

UIBackgroundTaskIdentifier backgroundTask;

當你在didReceiveRemoteNotification中收到APNS時寫這個,

if (!backgroundTask || backgroundTask == UIBackgroundTaskInvalid) {
        backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
                //do all clean up job here gets called few seconds before expiration
                [[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
                backgroundTask = UIBackgroundTaskInvalid;
        }];
    }

編輯

剛剛意識到你正在使用swift所以這里是代碼為你快速:)

在AppDelegate中聲明一個名為backgroundTask的變量,

var backgroundTask : UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid

並在你的didRecieveRemoteNotification中使用它,如下所示,

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {

    if let userInfo = userInfo as? [String: AnyObject] {
        let parseNotificationOperation = ParseNotificationOperation(userInfo: userInfo, fetchCompletionHandler: completionHandler)
        MainService.shared.enqueueApnsOperation(parseNotificationOperation)

        if (backgroundTask == UIBackgroundTaskInvalid) {
            backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler { () -> Void in
                self.endTask()
            }
        }
    }
}

最后寫一個方法,一旦你完成它就使你的到期處理程序失效:)

func endTask(){
   //do all your clean up here, this is called few seconds before the task expires.
   UIApplication.sharedApplication().endBackgroundTask(backgroundTask)
   backgroundTask = UIBackgroundTaskInvalid;
}

就是這樣:)快樂的編碼伙伴:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM