2013-01-03 20 views
12

是否有通過編程從Notification Tray中刪除/解除UILocalNotification的方法。 我能取消其去除以編程方式從通知托盤中刪除UILocalNotification

[[UIApplication sharedApplication] scheduledLocalNotifications] 

這裏的通知的通知是什麼,我需要做的

我需要拒絕來自NotificationTray的UILocalNotification的處理之後的動作(即用戶後點擊通知)

編輯: 我可以從NSNotificationCenter刪除通知。我想從通知紙盒中刪除特定通知。與用戶按下清除按鈕清除屬於特定應用程序的所有通知一樣。

+0

你的意思是通知中心? –

+1

我是指在應用程序外部顯示的通知托盤! –

回答

2
[UIApplication sharedApplication].applicationIconBadgeNumber = 0; 

會做一些伎倆

,但如果你因此未使用applicationIconBadgeNumber,它不會工作,那麼招設置 applicationIconBadgeNumber第一:)

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1]; 
1

如果應用程序沒有運行,您將收到本地通知對象

-applicationDidF inishLaunchingWithOptions:

,如:(UIApplication的*)應用didReceiveLocalNotification:

UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey]; 

否則你可以在

  • (無效)應用程序得到它(UILocalNotification *)通知

現在你可以使用通知中心中刪除它

[UIApplication的sharedApplication] cancelLocalNotification:notificationToCancel]。

15

可以使用取消所有通知:

[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

如果你想刪除特定的通知,可以使用通知對象的userinfo,當你創建一個本地通知添加一個唯一的ID了這一點。稍後,您可以使用該ID來刪除本地通知。

對於您可以使用下面的代碼:

NSString *notificationId = @"id_to_cancel"; 
UILocalNotification *notification = nil; 
for(UILocalNotification *notify in [[UIApplication sharedApplication] scheduledLocalNotifications]) 
{ 
    if([[notify.userInfo objectForKey:@"ID"] isEqualToString:notificationId]) 
    { 
    notification = notify; 
    break; 
    } 
} 
[[UIApplication sharedApplication] cancelLocalNotification:notification]; 
+9

如果通知已經顯示爲'scheduledLocalNotifications',將返回零條目(因爲它不再是'預定'並且已經被觸發),所以這不起作用。點擊時刪除通知的其他方式? – strange

5

,我相信我有一個類似的問題。當應用程序進入前臺時,我試圖清除過去的通知,以從通知托盤中刪除任何舊通知。

我做了這樣的事情搶老的通知並刪除它們:

NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
NSArray *pastNotifications = [activeNotifications filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"firDate < %@", [NSDate date]]]; 
for (UILocalNotification *notification in pastNotifications) { 
    [[UIApplication sharedApplication] cancelLocalNotification:notification]; 
} 

然而,似乎scheduledLocalNotifications不包括其大火日期已經過去,即使他們仍然會出現在通知中心位置。

調用cancelAllLocalNotifications似乎也刪除過去的通知。因此,我們可以抓取所有當前通知,取消所有內容,然後添加我們仍然感興趣的內容。

// Grab all the current notifications 
NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; 

// Clear all notifications to remove old notifications 
[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

// Add back the still relevant notifications 
for (UILocalNotification *notification in activeNotifications) { 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
} 

此外,我們還可以做的通知的一些過濾加入他們回來,如果一些不再需要之前,我們可以抓住主動通告,當應用程序被激活,將它們存儲在一個實例變量,只有加他們回來時,應用程序移動到後臺

+0

grt!日Thnx。它wrkd。 – mars

1
// deletes a pushnotification with userInfo[id] = id 
-(void)deleteLocalPushNotificationWithId:(NSString*)id{ 
    for(UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]){ 
    if([[notification.userInfo objectForKey:@"id"] isEqualToString:id]){ 
     [[UIApplication sharedApplication] cancelLocalNotification:notification]; 
    } 
    } 
} 

// deletes all fired pushnotifications 
-(void)clearLocalPushNotifications{ 
    NSArray *activeNotifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; 

    // Clear all notifications to remove old notifications 
    [[UIApplication sharedApplication] cancelAllLocalNotifications]; 

    // Add back the still relevant notifications 
    for (UILocalNotification *notification in activeNotifications) { 
    [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
    } 
} 
1

我與一些代碼擺弄,我想知道爲什麼本地通知存儲在通知中心,如果應用程序是在前臺。這可能是因爲蘋果公司不知道你在做什麼,並且不在乎;所以他們做他們的工作。

至於問題而言,我做了以下內容:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification 
{ 
    if (application.applicationState == UIApplicationStateActive) 
    { 
     NSLog(@"active"); 

     // display some foreground notification; 
     [application cancelLocalNotification:notification]; 
    } 
    else 
    { 
     NSLog(@"inactive"); 
    } 
} 
相關問題