如何避免ios中重复的UILocalNotification?

如何避免ios中重复的UILocalNotification?,ios,objective-c,xcode,uilocalnotification,Ios,Objective C,Xcode,Uilocalnotification,在我的应用程序中,我使用UILocalNotifications。但我的要求是,我想取消重复的通知 我的代码是 -(void)LocalNotificationMethod{ NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; // Get the current date NSDate *pickerDate = self.selectedDate; NSLog(@" self.s

在我的应用程序中,我使用UILocalNotifications。但我的要求是,我想取消重复的通知

我的代码是

-(void)LocalNotificationMethod{
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    // Get the current date
    NSDate *pickerDate =  self.selectedDate;
    NSLog(@" self.selectedDate %@", self.selectedDate);
    // Break the date up into components
    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )
                                                   fromDate:pickerDate];
    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                                   fromDate:pickerDate];

    // Set up the fire time
    NSDateComponents *dateComps = [[NSDateComponents alloc] init];
    [dateComps setDay:[dateComponents day]];
    [dateComps setMonth:[dateComponents month]];
    [dateComps setYear:[dateComponents year]];
    [dateComps setHour:[timeComponents hour]];
    // Notification will fire in one minute
    [dateComps setMinute:[timeComponents minute]];
    [dateComps setSecond:[timeComponents second]];
    NSDate *itemDate = [calendar dateFromComponents:dateComps];

    NSLog(@"itemDate %@",itemDate);
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if (localNotif == nil)
        return;
    localNotif.fireDate = itemDate;
      NSLog(@"itemDate %@", localNotif.fireDate);
    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    // Notification details
    localNotif.alertBody = [_titleTextFieldObj text];
    // Set the action button
    localNotif.alertAction = @"View";

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber =[[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

    NSLog(@" localNotif.applicationIconBadgeNumber ++ %ld", (long)localNotif.applicationIconBadgeNumber );



    // Specify custom data for the notification
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:[_titleTextFieldObj text] forKey:@"someKey"];

    localNotif.userInfo = infoDict;
    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
    //UILocalNotification *notif = [notificationArray objectAtIndex:indexPath.row];
    NSLog(@"notif %@",notificationArray);


    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];


   }
我是Objective-c的新手。我不知道在哪里编写代码来防止重复通知。有人能帮我解决这个问题吗


提前感谢。

本地通知具有一个
userInfo
属性,您可以使用该属性放置一些自定义信息,并可用于将通知配对。我建议您使用此信息插入包含通知类型的
NSDictionary
。例如:

NSString *newNotificationType = @"my_notification";
newNotification.userInfo = @{@"notification_type_key":newNotificationType};
然后,在实现此功能后,您可以创建一个检查,检查此类型的通知是否已存在,并取消上一个通知或取消创建新通知:

for(UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications])
{
    NSDictionary *userInfo = notification.userInfo;
    if([userInfo isKindOfClass:[NSDictionary class]] && [userInfo[@"notification_type_key"] isEqualToString:newNotificationType])
    {
        [[UIApplication sharedApplication] cancelLocalNotification:notification];
    }
}

当您将通知的重复间隔设置为每小时或每天重复一次时会怎么样。。??这将一次又一次地显示相同的通知,这很好。。。。但是有没有一种方法可以在发布新的重复通知时自动删除/取消旧的通知?我从未真正使用过这种方法,但您不能简单地删除旧的重复通知并创建一个新的通知吗?你可能会取消通知,然后修改相同的通知,并再次安排相同的通知对象,该对象现在已修改了值。我明白你的意思,但这是如果应用正在运行。当用户将通知设置为重复,然后关闭应用程序时会发生什么情况?然后通知会继续重复(这就是我想要的),但通知中心最终会有一长串重复的通知。必须有一种方法可以自动删除重复的通知(并保留最新的通知)。我不确定您是否真的可以这样做。通知中心是操作系统的一项功能,我找不到关于它的任何设置。您可以在代码中执行此操作,但必须激活应用程序。所以问题是如何使用本地通知激活应用程序(与推送通知一起使用,但也可能与本地通知一起使用),或者如何限制notification center中显示的通知数量(这似乎是操作系统设置)。是的,这很公平,首先,我会尽力限制我设置的本地通知的数量。谢谢