2013-03-09 30 views
0

我想將UILocalNotification添加到我的應用程序。 我想設置fireDate獲取時間和日期,並在特定分鐘後執行通知。例如,如果我給它的時間爲13:00,日期爲10/03/2013,我希望它在此時間後通知用戶10分鐘,15分鐘,30分鐘,45分鐘或1小時。 如何編碼?爲UILocalNotification設置fireDate

希望任何人都明白我的意思。 在此先感謝。

+0

您可以設置初始fireDate,但**沒有辦法**指定自定義重複間隔,例如見http://stackoverflow.com /問題/ 6966365/uilocalnotification重複基區間爲定製報警,太陽週一週二,週三,週四-F。 – 2013-03-09 09:38:15

回答

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]; 
} 
相關問題