0

我想取消UILocalnotification,當我取消通知仍然通知被解僱。我想知道我是否必須調用appdelegate中的任何委託方法來取消通知。我使用的代碼正在正確執行,但通知被解僱了。 我應該使用NSNotification中心有removeObserver方法取消uilocalnotification。NSNotificationCenter是否需要取消UILocalnotficiation?

UILocalnotification是否會觸發來自應用程序或設備的通知。

更新 - 這就是我如何安排我的通知

-(UILocalNotification *)scheduleNotification :(int)remedyID 
     { 
      NSString *descriptionBody; 

      NSInteger frequency; 

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

      NSLog(@"%d",remedyID); 

      descriptionBody =[[self remedyDetailsForRemedyID:remedyID] objectForKey:@"RemedyTxtDic"]; 
      frequency = [[[self remedyDetailsForRemedyID:remedyID] objectForKey:@"RemedyFrequency"]intValue]; 

      NSArray *notificationFireDates = [self fireDatesForFrequency:frequency]; 

      for (NSDate *fireDate in notificationFireDates) 
      { 
        notif.timeZone = [NSTimeZone defaultTimeZone]; 


        notif.repeatInterval = NSDayCalendarUnit; 
        notif.alertBody = [NSString stringWithString:descriptionBody]; 
        notif.alertAction = @"Show me"; 
        notif.soundName = UILocalNotificationDefaultSoundName; 

        notif.applicationIconBadgeNumber = 1; 

        notif.fireDate = fireDate; 

        NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:notif.alertBody,           @"kRemindMeNotificationDataKey", [NSNumber numberWithInt:remedyID],kRemindMeNotificationRemedyIDKey, 
               nil]; 

        notif.userInfo = userDict; 

        [[UIApplication sharedApplication] scheduleLocalNotification:notif]; 
       } 

       return notif; 

    } 

取消通知

- (void)cancelNotification:(int)remedyId 
    { 
    NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications]; 
    NSLog(@"Cancelling... Before %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]); 

     for (UILocalNotification *notification in notifications) 
     { 

     int notifRemedyId = [[notification.userInfo objectForKey:@"kRemindMeNotificationRemedyIDKey"]intValue]; 
     NSLog(@"remedyID : %d",remedyId); 
     NSLog(@"notifyId : %d",notifRemedyId); 
     if (remedyId == notifRemedyId) { 
      [[UIApplication sharedApplication] cancelLocalNotification:notification]; 
      } 
     } 

    NSLog(@"Cancelling... After %d",[[[UIApplication sharedApplication]scheduledLocalNotifications]count]); 

    } 

回答

0

scheduledLocalNotifications會給你所有的計劃通知的列表,並使用

- (void)cancelLocalNotification:(UILocalNotification *)notification 

或你可以使用全部取消:

[[UIApplication sharedApplication] cancelAllLocalNotifications]; 

編輯:

NSString *notifRemedyId = @"notifRemedyId"; 
UILocalNotification *localnotif=[[UILocalNotification alloc]init]; 

NSDictionary *userDict = [NSDictionary dictionaryWithObjectsAndKeys:@"kRemindMeNotificationRemedyIDKey", kRemindMeNotificationRemedyIDKey,YOUR_REMEDYID,@"notifRemedyId",nil]; 
localnotif.userInfo = userDict; 
[[UIApplication sharedApplication] scheduleLocalNotification:localnotif]; 

取消爲:

for (UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) { 
    if ([[aNotif.userInfo objectForKey: kRemindMeNotificationRemedyIDKey] isEqualToString:@"kRemindMeNotificationRemedyIDKey"]) { 
     if (remedyId == [[aNotif.userInfo objectForKey: kRemindMeNotificationRemedyIDKey] intValue]) { 
      [[UIApplication sharedApplication] cancelLocalNotification:aNotif]; 
      break; 
     } 
    } 
} 

希望它可以幫助你。

+0

是否強制將通知作爲參數傳遞,或者它可以成爲補救ID ..因爲我傳遞remedyID原因,如果remedyID與notificationID相同,那麼我取消通知..但我仍然收到通知 – raptor 2013-05-06 17:13:22

+0

是的,您需要通過通知。檢查我更新的答案。 – 2013-05-06 17:18:48

+0

我編輯了我的答案。檢查出來.. – 2013-05-06 17:23:05

1

NSNotification center has removeObserver方法取消uilocalnotification。

NSNotificationCenter有沒有與UILocalNotification有關。我很抱歉他們都在他們的名字中使用了「通知」,但這真的只是巧合。

+0

好吧謝謝澄清@matt – raptor 2013-05-06 17:32:27