从选择器中选择的时间每天发出本地通知

时间:2011-07-22 07:01:36

标签: iphone objective-c

在我的应用程序中,我想在特定时间每天设置一个本地通知。时间是从时间选择器中选择的那个。是否有任何方法。请帮助我。

2 个答案:

答案 0 :(得分:1)

只需将fireate指定为UIDatePicker和NSDayCalendarUnit中的日期作为repeatinterval:

UILocalNotification *localNotif = [[UILocalNotification alloc] init];

if (localNotif == nil)
    return;

localNotif.fireDate = datePicker.date;
localNotif.timeZone = [NSTimeZone defaultTimeZone];

localNotif.alertBody = @"This is the Alert-Body Text"];
localNotif.alertAction = @"Button-Text";

localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;

localNotif.repeatInterval = NSDayCalendarUnit;

[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];

答案 1 :(得分:0)

您可以设置本地通知。 苹果的示例代码:

- (void)scheduleNotificationWithItem:(ToDoItem *)item interval:(int)minutesBefore {
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:item.day];
    [dateComps setMonth:item.month];
    [dateComps setYear:item.year];
    [dateComps setHour:item.hour];
    [dateComps setMinute:item.minute];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];
    [dateComps release];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];
    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i minutes.", nil),
         item.eventName, minutesBefore];
    localNotif.alertAction = NSLocalizedString(@"View Details", nil);

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;

    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:item.eventName forKey:ToDoItemKey];
    localNotif.userInfo = infoDict;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];
}

您可以为特定的timstamp设置本地通知。 您必须在applicationDidFinishedLaunching中检查 userinfo 字典中的任何通知数据。

你必须处理:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
相关问题