【问题标题】:iOS local notification custom dataiOS本地通知自定义数据
【发布时间】:2015-02-05 22:21:33
【问题描述】:

我每天上午 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];

【问题讨论】:

    标签: ios objective-c xml-parsing uilocalnotification


    【解决方案1】:

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

    【讨论】:

    • 是的,我知道,但我试图在没有 APNS 的情况下完成此任务,而只有本地。
    • 我不相信你可以在本地通知中执行任务。您不能为操作系统“安排工作”。所以恐怕你想要达到的目标是不可能的。
    【解决方案2】:

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

    注意:Apple 最多允许 30 秒在后台执行任务

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多