0

我想取消 UILocalnotification ,当我取消通知时,通知仍然被触发。我想知道我是否必须调用 appdelegate 中的任何委托方法来取消通知。我正在使用的代码正在正确执行。但是通知被触发了。我应该使用具有 removeObserver 方法的 NSNotification 中心来取消 uilocalnotification。

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]);

     }
4

2 回答 2

1

NSNotification 中心有 removeObserver 方法来取消 uilocalnotification。

NSNotificationCenter与 UILocalNotification 无关很抱歉,他们都在名字中的某处使用了“通知”,但这只是巧合。

于 2013-05-06T17:19:55.680 回答
0

scheduleLocalNotifications将为您提供所有计划通知的列表并使用

- (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;
        }
    }
}

希望它可以帮助你。

于 2013-05-06T16:48:23.847 回答