收到apns时的后台任务 [英] Background task when apns received

查看:38
本文介绍了收到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.

这是我在通知中的字典:

This is my dictionary inside the notification:

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

如果我的应用程序在前台,我会执行以下代码:

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);  
}

但是当app在后台时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.

有什么办法吗?

非常感谢!

推荐答案

是的,从 iOS 7 开始就可以了,您需要将 remote-notification 添加到 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 .

然后实现application: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:

处理程序

下载操作完成后要执行的块.调用该块时,传入最好的fetch结果值描述下载操作的结果.你必须调用这个处理程序,并应尽快这样做.对于可能的列表值,请参阅 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天全站免登陆