簡體   English   中英

創建每天從明天開始的本地通知

[英]Create a daily local notification that will start tomorrow

我有一個應用程序,可以每天提醒用戶有關待辦事項的信息,但是如果他們已經做過,則不需要提醒他們。

我解決問題的方法是創建一個本地通知,該通知每天隨機觸發。 當一天中有一件待辦事項已經完成時,我通過取消它並創建一個明天的新日期來將本地通知日重新安排為明天。 但是此解決方案不起作用。 似乎當您指定每天觸發一次本地通知(repearInterval = NSDayCalendarUnit)時,它不遵守fireDate day屬性。

這是我的代碼:

/*** notification is created like this ***/
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [self randomHourMinFromDate:[NSDate date]];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = @"A todo item is due!";
localNotif.alertAction = @"View Item";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.repeatInterval = NSDayCalendarUnit;


/*** some other part of the code ***/
NSArray *scheduledNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notification = [scheduledNotifications firstObject];

// adds 1 day to notification fireDate
NSDate *newFireDate = [notification.fireData dateByAddingTimeInterval:60*60*24*1];

[[UIApplication sharedApplication] cancelLocalNotification:notification];
notification.fireDate = newFireDate;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];

你能告訴我我做錯了嗎? 還是建議更好的解決方案? TIA!

我有個主意 當一天的待辦事項已經完成時,請取消該通知並創建一個新的通知。 將firedate設置為與當前時間相同的日期,並設置dateByAddingTimeInterval:60 * 60 * 24。

localNotification.fireDate = [CurrentDate dateByAddingTimeInterval:60*60*24];

可能這可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM