当收到APNS后台任务 [英] Background task when apns received

查看:232
本文介绍了当收到APNS后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读APNS文件之后,我已经实现APNS到我的应用程序。一切工作正常,但我有一个问题。
我不知道这是否是可能的,但我喜欢做任务的时候我的应用程序在后台,我收到APNS通知。

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 a task when my app is in background and I receive a apns notification.

这是我的字典里通知:

aps =
{
  alert = "message";
  sound = "default";
};

如果我的应用程序是在前台,我执行此code:

If my app is in foreground, I execute this code:

- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
{     
    [HTTPConnection sendGetToUrl:[NSURL URLWithString:stringUrl]];

    NSLog(@"Received notification: %@", userInfo);  
}

但是,APNS不执行didReceiveRemoteNotification当应用程序在后台,所以我想执行方法:

But apns does not execute "didReceiveRemoteNotification" when the app is in background, so I would like to execute the method:

[HTTPConnection sendGetToUrl:[NSURL URLWithString:stringUrl]];

如果我收到的背景APNS通知。

if I receive an apns notification in background.

有没有办法做到这一点?

Is there any way to do it?

太感谢了!

推荐答案

是的,这是更多钞票,因为iOS的7,您将需要远程通知添加到 UIBackgroundModes 在你的应用的info.plist

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

然后实现<一个href=\"https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40006786-CH3-SW45\"相对=nofollow> 应用程序:didReceiveRemoteNotification:fetchCompletionHandler: 在你的应用程序代理。您将有30秒的时间获取数据,如果你的应用程序需要更长的时间,系统可能会终止你的。

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:

处理程序

该块在下载操作完成后执行。
  当调用该块,通过在获取结果值最
  描述你的下载操作的结果。你必须把这个
  处理程序,并应使尽快做。对于可能的列表
  值,请参阅<一个href=\"https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/c/tdef/UIBackgroundFetchResult\"相对=nofollow> UIBackgroundFetchResult 类型。

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.

这篇关于当收到APNS后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆