2013-04-09 64 views
0

我提醒應用程序有表格視圖,並且單元格中有日期,並且該單元格日期成爲今天的日期UILocalNotification會觸發。對於我使用下面的代碼刪除包含日期的單元格時的UILocalNotification

-(void)notification { 

    // logic for local notification start 

    NSDateFormatter *Form = [[NSDateFormatter alloc] init]; 
    [Form setDateFormat:@"dd/MM/yyyy"]; 

    UILocalNotification *notification = [[UILocalNotification alloc] init]; 
    for (int i=0;i<_convertedBdates.count;i++) 
    { 
     NSDate *date =[Form dateFromString:[_convertedBdates objectAtIndex:i ]]; 
     //  NSLog(@"date%@",date); 

     if(notification) 
     { 
      notification.fireDate = date; 
      notification.timeZone = [NSTimeZone defaultTimeZone]; 
      notification.alertBody = [NSString stringWithFormat:@"Today is %@\'s Birthday",[_combinedNameArray objectAtIndex:i]]; 
      notification.alertAction = @"View"; 

      notification.soundName = UILocalNotificationDefaultSoundName; 
      notification.applicationIconBadgeNumber = 1; 
      [[UIApplication sharedApplication] scheduleLocalNotification:notification]; 
     } 
    } 
    // local notification logic ends here 

} 

現在我還實施了從表視圖 刪除細胞的功能,現在我的問題是細胞被刪除,但其通知亦不存在任何細胞,但是當這個日期來再通知消防。

當該單元格被刪除時,我應該如何刪除該特定通知?

回答

1

編輯:我最初的回答是錯誤的,因爲我沒有意識到操作系統在您安排時複製UILocalNotification,而不是保留它們。所以......

據我所知,有兩種方法可以做到這一點。

  • 當一行被刪除時取消所有通知,然後重新安排其餘的通知。

如果您沒有安排很多通知,這樣會更有效率,而且編碼一定會更容易。 (注意:我對工作中的低級別事情不夠了解,認爲哪一項效率更高,但我的猜測是差異並不重要)。

每當刪除一行時,只需簡單地調用

[[UIApplcation sharedApplication] cancelAllLocalNotifications]; 

然後適當地更新_convertedBDates,最後又打電話給你notification方法來重新安排新的本地通知對於那些仍然存在的事件。

  • 您的本地通知

這可能是更有效的方式,如果你能想出一個好辦法,使這些唯一的標識符,如果你有很多的通知計劃創建唯一標識符。 (重點在可能)。一種可能性是使用通知將觸發的時間,如果您可以保證沒有兩個通知會同時觸發。其他可能性是通知的標籤(如果您確信唯一性,也是如此)。無論你決定使用您的唯一識別碼,您可以通過添加這是你外面的for循環將其存儲:

self.uniqueIDArray = [[NSMutableArray alloc] init]; 

(其中uniqueIDArray是你的類的NSMutableArray* @property),然後這對你安排通知之前:

[uniqueIDArray addObject:whateverObjectYouUseForTheUniqueID]; 
notification.userInfo = [[NSDictionary alloc] initWithObjects:whateverObjectYouUseForTheUniqueID 
                 forKeys:@"uniqueID"]; 

然後,在你使用刪除單元格的任何方法,你會做這樣的事情:

uniqueIDToDelete = [self.uniqueIDArray objectAtIndex:indexOfCellBeingDeleted]; 
NSArray *scheduledNotifications = [[UIApplication sharedApplication] scheduledNotifications]; 
UILocalNotification *notifToDelete; 
for (UILocalNotification *notif in scheduledNotifications) { 
    if ([[notif.userInfo objectForKey:@"uniqueID"] isEqual:uniqueIDToDelete]) { 
     [[UIApplication sharedApplication] cancelLocalNotification:notif]; 
    } 
} 
[self.uniqueIDArray removeObjectAtIndex:indexOfCellBeingDeleted]; 
+0

就可以證明這一點,請用一些例子嗎? – 2013-04-09 07:44:26

+0

什麼部分不清楚? – drewmm 2013-04-09 14:04:57

+0

它只是2個月我已經開始iPhone所以除了cancellocalnotification一切都不清楚即時通訊 – 2013-04-10 04:43:12

1

我認爲你不應該使用電池爲s通知。改用數據模型。你也可以刪除通知容易

UPDATE

Model *model = [_array objectAtIndex:indexPath.row]; model.notificationID = // here you store created notification identifier 

[[UIApplication sharedApplication] scheduledLocalNotifications] // contains all local notifications, you should search you need by ID and remove it using cancelLocalNotification method. 

附:您可以在通知存儲用戶信息

+0

我nood你可以舉一些例子,請我使用數組我用於表視圖 – 2013-04-09 07:43:03

0

通知ID試試這個

NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
UILocalNotification *notif = [notificationArray objectAtIndex:indexPath]; 
[[UIApplication sharedApplication] cancelLocalNotification:notif]; 
相關問題