iOS本地通知自定义数据

时间:2015-02-05 22:21:34

标签: ios objective-c xml-parsing uilocalnotification

我每天上午9点都会以汇率显示当地通知。但如果(例如欧元)的价值与昨天的价值相同则不应该触发。我正在使用XML从我的服务器下载数据。所以我的问题是有可能解析XML数据(每天上午9点显示通知之前),而应用程序没有运行(被任务管理器杀死),因为显示通知(即使应用程序被完全杀死)。

这就是我发布通知的方式:

NSDate *now = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
[components setHour:9];
// Gives us today's date but at 9am
NSDate *next9am = [calendar dateFromComponents:components];
if ([next9am timeIntervalSinceNow] < 0) {
    // If today's 9am already occurred, add 24hours to get to tomorrow's
    next9am = [next9am dateByAddingTimeInterval:60*60*24];
}

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = next9am;
notification.alertBody = @"Euro value: <PARSED VALUE HERE>";
// Set a repeat interval to daily
notification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

2 个答案:

答案 0 :(得分:0)

如果您可以向您的应用发送APNS推送通知,您可以从后台唤醒它并让它处理您的xml(例如当汇率更改时)并安排本地通知。

答案 1 :(得分:0)

您必须使用静音推送通知。它将在不知道用户的情况下在后台启动您的应用程序,然后您可以下载xml并进行比较。如果它是相同的,则向用户显示本地通知,然后用户可以继续进行。

注意:Apple提供最多30秒的时间来执行后台任务

相关问题