2014-01-29 39 views
1
UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
localNotification.alertBody =[NSString stringWithFormat:@"meeting schedule from %@ ",[namelist objectAtIndex:i]]; 
localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
localNotification.soundName = @"meetting.mp3"; 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 
[[NSNotificationCenter defaultCenter] postNotificationName:@"timerInvoked" object:self]; 

我正在使用上面的代碼來創建本地通知。但以前的通知不會被清除。當新的通知被觸發時,它再次觸發。在觸發後刪除UILocalNotification

我用[[UIApplication sharedApplication] cancelAllLocalNotifications];

通知不回地面發射。如何在通知發佈後刪除通知?

+0

如果要設置'的本地通知firedate'?你真的收到通知了嗎? –

+0

我正在使用本地通知將信息發送到服務通知 – Anupama

+0

我想你對本地通知沒有明確的想法。我不確定你想要UILocalNotification或NSNotification。你能澄清你的要求嗎? –

回答

0

cancelAllLocalNotifications刪除待處理的通知。我認爲,一旦他們被解僱,您可以從通知中心刪除通知。用戶設置(通過設置應用程序)他們想要保留的通知的數量,並且一旦數量超過該限制,它們將被自動移除。

+0

我是新的蜜蜂給ios.I使用本地以下情況的通知。我正在創建聊天應用程序..當消息從服務到達時,我將信息發送到通知。它顯示在通知中,但是當發生新數據時,它會發送帶有舊消息的新消息 – Anupama

+0

是否僅僅是您不刪除通知(或從通知中打開應用程序)。所以當收到另一條消息時,你現在有兩個可見的通知?這應該是... – Wain

+0

你可能想看看使用'applicationIconBadgeNumber'來顯示有多少條未讀消息。 – Wain

0

當應用程序收到本地通知時,將調用以下方法。把它放在應用程序的委託:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
    UIApplication *app = [UIApplication sharedApplication]; 
    NSInteger badgeNumber = [app applicationIconBadgeNumber];// Take the current badge number 
    badgeNumber--; // decrement by one 
    [app setApplicationIconBadgeNumber:badgeNumber]; // set ne badge number 
    [app cancelLocalNotification:notification]; // cancel the received notification. It will clear the notification from banner alos 
} 
+0

此代碼工作與以前一樣它用舊信息顯示 – Anupama

0

如果您希望能夠刪除特定的通知,他們已經解僱後,你需要存儲。

後:

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

也將其保存在NSUserDefaults的,使用的NSKeyedArchiver將其轉換爲NSData的。

NSData *notificationData = [NSKeyedArchiver archivedDataWithRootObject:localNotification]; 

[[NSUserDefaults standardUserDefaults] setObject:notificationData forKey:[NSString stringWithFormat:@"%d", someKeyOfYourChoice]]; 

爲了讓它恢復爲使用NSKeyedUnarchiver的UILocalNotification。然後,您可以使用cancelLocalNotification方法將其刪除。

進一步解釋here(雨燕版+鏈接到原來的OBJ-C溶液)