6

我知道这个问题在StackOverflow上问了很多次,但我无法在我的应用程序中设置警报,因为我对 iOS 很陌生?我正在按照本教程设置警报:

在 iOS 中使用 UILocalNotification 设置提醒。

但是,它似乎对我不起作用。

我需要每天设置闹钟让我们5.00 PM每天说。我不能使用日期选择器来选择时间。

4

4 回答 4

9
  1. 首先在您的xib, (或代码)上设置日期选择器模式:时间(默认为日期和时间)

  2. 系统假定触发日期是当前日期,时间是用户选择的时间。这不是问题,因为您设置了重复间隔,所以它会起作用。我已经测试过了。

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    [localNotif setFireDate:datePicker.date];
    [localNotif setRepeatInterval:NSDayCalendarUnit];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    

PS:最好将秒设置为0using NSDateComponentsclass,以便将闹钟设置为在您想要的一分钟的第一秒响起。您可以检查:

iOS 中的本地通知。

您发布的有关如何执行此操作的教程。

于 2012-08-07T17:59:48.633 回答
1
+ (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID

使用参数调用此方法并使用此

 + (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

//set the notification date/time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:day];

[dateComps setMonth:month];

[dateComps setYear:year];
[dateComps setHour:hours];

[dateComps setMinute:minutes];
[dateComps setSecond:seconds];

NSDate *notificationDate = [calendar dateFromComponents:dateComps];
[dateComps release];

UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
    return;
localNotif.fireDate = notificationDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];

// Set notification message
localNotif.alertBody = alertBody;
// Title for the action button
localNotif.alertAction = actionButtonTitle;

localNotif.soundName = (alertSoundName == nil) ? UILocalNotificationDefaultSoundName : alertSoundName;

//use custom sound name or default one - look here to find out more: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html%23//apple_ref/doc/uid/TP40008194-CH103-SW13

localNotif.applicationIconBadgeNumber += 1; //increases the icon badge number

// Custom data - we're using them to identify the notification. comes in handy, in case we want to delete a specific one later
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
localNotif.userInfo = infoDict;

// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
于 2012-08-09T12:34:13.430 回答
0

You may need to change the style of the date picker to allow changing the time in addition to just the date.

于 2012-08-07T16:02:05.773 回答
0

你可以试试这个

UILocalNotification *todolistLocalNotification=[[UILocalNotification alloc]init];
[todolistLocalNotification setFireDate:[lodatepicker date]];
[todolistLocalNotification setAlertAction:@"Note list"];
[todolistLocalNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[todolistLocalNotification setAlertBody:text_todolist];
[todolistLocalNotification setHasAction:YES];
于 2013-07-05T07:01:56.860 回答