应用处于后台状态时获取本地通知?

时间:2013-04-04 14:45:26

标签: ios nsdate background-process uilocalnotification

我想开发一个提醒应用。它应该每周,每月,每季度,每六个月和每年提醒人们。

我使用NSDate属性获取当前日期,然后我将7天添加到当前日期,添加7天后的日期将存储在plist中。

我选择电话/邮件/留言按钮提醒人们。在按钮操作中,我将当前日期与plist中保存的日期进行比较,然后设置本地通知。但根据开火日期,当地通知不起作用。当应用程序位于前台时,它工作正常,但它进入后台却无法正常工作。

1 个答案:

答案 0 :(得分:0)

您必须安排闹钟。以下是Apple文档的摘录:

- (void)scheduleAlarmForDate:(NSDate*)theDate
{
    UIApplication* app = [UIApplication sharedApplication];
    NSArray*    oldNotifications = [app scheduledLocalNotifications];

    // Clear out the old notification before scheduling a new one.
    if ([oldNotifications count] > 0)
        [app cancelAllLocalNotifications];

    // Create a new notification.
    UILocalNotification* alarm = [[UILocalNotification alloc] init];
    if (alarm)
    {
        alarm.fireDate = theDate;
        alarm.timeZone = [NSTimeZone defaultTimeZone];
        alarm.repeatInterval = 0;
        alarm.soundName = @"alarmsound.caf";
        alarm.alertBody = @"Time to wake up!";

        [app scheduleLocalNotification:alarm];
    }
}

您可以在Apple的文档"App States and Multitasking"中找到很多有用的信息,特别是在"Background Execution and Multitasking"部分(它完全涵盖您的用例)。

相关问题