1

我想知道是否可以設置結束日期爲UILocalNotificationUILocalNotification結束日期

我想我的通知每天開火(NSDayCalendarUnit),但我有我不能跨越的截止日期(截止日期),例如我每天都會拍一張我長大的小鬍子的照片,一年後會不會顯示通知。

我希望你有我的觀點......

回答

4

沒有在UILocalNotification這樣的選擇,因爲你可以在文檔中讀取。

您唯一的選擇是每當用戶啓動應用程序時檢查一年是否結束。

+0

...並在檢查後決定是否應該取消通知。這個答案不是我想聽到的,但現在我確信我不能實現這一點。謝謝@rckoenes – cojoj

-3
Use following code: 

UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
localNotification.repeatInterval = NSDayCalendarUnit; 
+0

我知道如何管理'repeatInterval'屬性,這不是我所問的。 – cojoj

1

UILocalNotification對象,我建議設置repeatInterval財產,並把結束日期在userInfo字典查詢以後,以確定是否通知已過期。例如:

UILocalNotification* uiLocalNotification; 
uiLocalNotification = [[UILocalNotification alloc] init]; 

//Set the repeat interval 
uiLocalNotification.repeatInterval = NSCalendarUnitDay; 

NSDate* fireDate = ... 

//Set the fire date 
uiLocalNotification.fireDate = fireDate; 

//Set the end date 
NSDate* endDate = ... 

uiLocalNotification.userInfo = @{ 
            @"endDate": endDate 
           }; 

UIApplication* application = [UIApplication sharedApplication]; 
[application scheduleLocalNotification:uiLocalNotification]; 

//... 

//Somewhere else in your codebase, query for the expiration and cancel, if necessary 

UIApplication* application = [UIApplication sharedApplication]; 
NSArray* scheduledNotifications = application.scheduledLocalNotifications; 

NSDate* today = [NSDate date]; 

for (UILocalNotification* notification in scheduledNotifications) { 

    NSDate* endDate = notification.userInfo[@"endDate"]; 

    if ([today earlierDate:endDate] == endDate) { 
     //Cancel the notification 
     [application cancelLocalNotification:notification]; 
    } 
}