1

I am trying to cancel a already scheduled notification , notification is getting called but when i try to cancel a notification its not getting cancelled .

NSArray notification contains some random values when there is only one scheduled notification. can anyone help me . I am want to cancel the notification for a particular bookID.

UPDATE :

 -(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)bookID
{
    int notificationBook=0;

        NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];

    for (UILocalNotification *notification in notifications)
    {
        int notifBookId = [[notification.userInfo objectForKey:kRemindMeNotificationBookIDKey] intValue];


        for (int i=0; i<[bookArray count]; i++)
        {
            notificationBook =[[[remedyArray objectAtIndex:i] objectForKey:@"BookID"] intValue];
        }

        NSLog(@"%d",[[notification.userInfo objectForKey:kRemindMeNotificationBookIDKey]intValue]);

        NSLog(@"%d",notifBookId);

        if (bookId == notifBookId)
        {
            [[UIApplication sharedApplication] cancelLocalNotification:notification];
        }
    }

}
4

2 回答 2

2

当只有一个计划通知时,NSArray 通知包含一些随机值。

这可能是由于该应用程序的某些先前计划的通知已经存在。一旦尝试取消应用程序的所有通知再次启动

   [[UIApplication sharedApplication] cancelAllLocalNotifications];  

你的主要问题

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


 }

你给的钥匙错了。我认为那是你的问题。我又发现了一件事,您将每个通知安排两次,我不知道为什么。检查一下。

于 2013-04-18T07:21:15.477 回答
0

您应该将检查移到循环内部

for (int i=0; i<[bookArray count]; i++)
{
    int bookId =[[[remedyArray objectAtIndex:i] objectForKey:@"BookID"]    intValue];

    if (bookId == notifBookId)
    {
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
    }
}
于 2013-04-18T07:39:24.790 回答