2013-05-03 51 views
1

我想取消UILocalnotification。我已經創建了一個名爲cancelNotification的方法,用於取消通知。然而,即使取消後我仍然收到通知 我聽到在應用程序委託在幾個代表方法,我們需要取消通知。任何人都可以解釋我在哪裏所有的調用取消方法..如果任何人都可以幫助我,這將是偉大的在目標C中取消UILocalnotification的步驟C

-(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]; // I change the key value 

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

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

    } 
+0

當您取消通知時會放置一個斷點,如果該通知被取消或未被取消。 – 2013-05-03 17:36:30

+0

你想收集所有通知嗎?那些NSlog的輸出是什麼? – 2013-05-03 17:37:01

+0

NSLog顯示正確的值..我想知道我是否必須在應用程序代表中更新?... – raptor 2013-05-03 17:47:12

回答

1

[[UIApplication sharedApplication] cancelAllLocalNotifications];

只是複製下,甚至在你的下一個UILocalNotification代碼的方法didLaunchWithOptions或viewDidLoad中。

我相信這就是你在代表的意思嗎?如果您在應用程序啓動後調用此方法,它將取消您之前的所有本地通知。

+0

請參閱我何時在特定時間添加通知以激發...當用戶關閉應用程序並重新啓動通知時應該激發..這就是爲什麼我不調用'[[UIApplication sharedApplication] cancelAllLocalNotifications];'在viewdidload或appdelegate中。 我的問題是從應用程序或從設備發射uilocalnotiification,如果我取消通知它會被解僱? – raptor 2013-05-05 02:40:39

+0

@raptor我不確定,但我認爲如果它是'重複時間間隔',那麼它來自設備,因爲它會在'firedate'觸發器之後立即執行。如果您取消通知,它將不會觸發,除非您再次重新安排時間。 – user1949873 2013-05-05 02:58:02

+0

@ user1949873 .....即使我取消通知後,通知仍然在發射...... – raptor 2013-05-06 08:22:10