2012-11-02 67 views
1

我有一個很大的問題!我的問題是,我在我的委託中實現了localnotification代碼。我在didEnterBackround方法中放入了這段代碼。但是,我爲每個didEnterBackround獲得了一個localnotifi。如果最新消息已經消失,是否有一種可能的方式只有一個新的通知開始啓動?就像我有一個5天后發生的本地非點擊。當我打開和關閉應用程序5次,並等待5天,我得到5個本地通知。是否有可能讓應用程序只有一個被解僱,在這之後出現一個新的得到startet?!只讓本地通知出現一次

我的代碼:

NSCalendar *calender = [NSCalendar autoupdatingCurrentCalendar]; 
NSDate *currentDate = [NSDate date]; 

NSDateComponents *dateComponents = [calender components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:currentDate]; 

NSDateComponents *timeComponents = [calender components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:currentDate]; 

NSDateComponents *temp = [[NSDateComponents alloc]init]; 

[temp setYear:[dateComponents year]]; 
[temp setMonth:[dateComponents month]]; 
[temp setDay:[dateComponents day]]; 
[temp setHour:[timeComponents hour]]; 
[temp setMinute:[timeComponents minute]]; 
[temp setSecond:[timeComponents second] +10]; 

NSDate *fireTime = [calender dateFromComponents:temp]; 


UILocalNotification *localN = [[UILocalNotification alloc]init]; 
localN.fireDate = fireTime; 
localN.timeZone = [NSTimeZone defaultTimeZone]; 


localN.alertBody = @"BLA BLA BLA!"; 
localN.alertAction = @"GO BACK"; 


localN.applicationIconBadgeNumber = 1; 


[[UIApplication sharedApplication] scheduleLocalNotification:localN]; 

回答

4

你可以取消所有計劃通知:

[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

如果你不想刪除所有計劃通知(也許你還有別的安排) ,那麼看看cancelLocalNotification:,它允許你只取消一個實例。

+0

你能爲我做一個類似於代碼片段的東西嗎?我真的不明白你的意思......或者我應該把這些代碼放在哪裏?對不起,因爲我是新手 – MasterRazer

+0

Put'[[UIApplication sharedApplication] cancelAllLocalNotifications];'RIGHT超出你給我們的代碼。 (在NSCalendar行之上)。它會按你的意願工作。 –

+0

感謝您的快速響應我會這樣做 – MasterRazer