我正在为我正在开发的应用程序添加本地通知。我只在 2013 年 4 月 30 日纽约/东部时间晚上 11:00 设置了一个通知。我该怎么做?我尝试了多种方法,但没有一种方法能正常工作。这是我目前正在使用的(它不起作用):
- (void)applicationDidEnterBackground:(UIApplication *)application
{
if (![@"1" isEqualToString:[[NSUserDefaults standardUserDefaults]
objectForKey:@"setNotify"]]) {
[[NSUserDefaults standardUserDefaults] setValue:@"1" forKey:@"setNotify"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSString *str = [NSString stringWithFormat:@"2013-04-23T18:22:00"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"US/Eastern"]];
NSDate *dte = [dateFormat dateFromString:str];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[cal setTimeZone:[NSTimeZone timeZoneWithName:@"US/Eastern"]];
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[UILocalNotification alloc]
init];
if (notifyAlarm)
{
notifyAlarm.fireDate = dte;
notifyAlarm.timeZone = [NSTimeZone timeZoneWithName:@"US/Eastern"];
notifyAlarm.repeatInterval = 0;
notifyAlarm.soundName = @"trumpet.m4a";
notifyAlarm.alertBody = @"Message";
[app scheduleLocalNotification:notifyAlarm];
}
}
}