Background task when apns received

前端 未结 3 793
情书的邮戳
情书的邮戳 2020-12-20 00:35

After reading APNS documentation, I have implemented apns into my app. Everything works fine, but I have a question. I don\'t know if it is possible, but I would like to do

3条回答
  •  遥遥无期
    2020-12-20 00:55

    Yes this is posible since iOS 7, you will need to add remote-notification to the UIBackgroundModes in your apps info.plist .

    Then implement the application:didReceiveRemoteNotification:fetchCompletionHandler: in your app delegate. You will have 30 seconds to fetch data, if you app takes longer the system might terminate your.

    - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))handler {
       NSString *stringURl = /* some URL */;
       [HTTPConnection sendGetToUrl:[NSURL URLWithString:stringUrl]];
    
       // Once done call handler with one of the result types:
       // UIBackgroundFetchResultNewData, UIBackgroundFetchResultNoData, UIBackgroundFetchResultFailed
    
       handler(UIBackgroundFetchResultNewData);
    }
    

    You will have to notify the system when the call is done and that data is fetched by call the handler. As stated by the documentation you will need to tell the handler if you succeeded to fetch any data:

    handler

    The block to execute when the download operation is complete. When calling this block, pass in the fetch result value that best describes the results of your download operation. You must call this handler and should do so as soon as possible. For a list of possible values, see the UIBackgroundFetchResult type.

提交回复
热议问题