アプリにUILocalNotificationを追加したい。日時を取得して特定の分後に通知を行うようにfireDateを設定したいと思います。たとえば、時刻を13:00とし、日付を2013年10月3日とし、この時刻以降に10分、15分、30分、45分、または1時間でユーザーに通知したい場合です。これをコーディングする方法は?
誰かが私の意地悪を理解したことを願っています。前もって感謝します。
アプリにUILocalNotificationを追加したい。日時を取得して特定の分後に通知を行うようにfireDateを設定したいと思います。たとえば、時刻を13:00とし、日付を2013年10月3日とし、この時刻以降に10分、15分、30分、45分、または1時間でユーザーに通知したい場合です。これをコーディングする方法は?
誰かが私の意地悪を理解したことを願っています。前もって感謝します。
これが私が得た最良の答えです
- (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];
}