Ios 取消并重新安排UILocalNotification的重复间隔将再次触发

Ios 取消并重新安排UILocalNotification的重复间隔将再次触发,ios,uilocalnotification,Ios,Uilocalnotification,我正在做报警应用程序,在我的应用程序中我已经被解雇了 UILocalNotification具有重复间隔(同样:每个 日、工作日等),在发射后,我将取消所有 通知并再次重新安排所有通知,但相同 已经触发的通知将再次出现,而不是它应该出现 为它的未来而来,它有下一个开火日期,但它的到来 再说一遍 我的代码: -(void) setAlarmNotification:(NSDictionary *)dictionaryForAlarm { UILocalNotification *loca

我正在做报警应用程序,在我的应用程序中我已经被解雇了 UILocalNotification具有重复间隔(同样:每个 日、工作日等),在发射后,我将取消所有 通知并再次重新安排所有通知,但相同 已经触发的通知将再次出现,而不是它应该出现 为它的未来而来,它有下一个开火日期,但它的到来 再说一遍

我的代码:

-(void) setAlarmNotification:(NSDictionary *)dictionaryForAlarm {

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

    [localNotification setUserInfo:dictionaryForAlarm];
    [localNotification setAlertBody:[NSString stringWithFormat:kDefaultNotificationMessage]];
    [localNotification setSoundName:[NSString stringWithFormat:@"%@.%@",[dictionaryForAlarm objectForKey:kAlarmSoundName],[[GlobalClass getSoundDetails:[dictionaryForAlarm objectForKey:kAlarmSoundName]] objectForKey:kSoundExtention]]];
    [localNotification setTimeZone:[NSTimeZone localTimeZone]];

    // setting weather to provide snooze option or not
    if([[dictionaryForAlarm objectForKey:kAlarmSnooze] integerValue]){

        [localNotification setCategory:kNotificationCategoryIdentity];
    }

    // Set Notification with repeat/ also check its not firing the snooze notification
    if(([[dictionaryForAlarm objectForKey:kAlarmRepeatState] intValue] == ALARMREPEATSTATEE_ON) && ([[dictionaryForAlarm objectForKey:kAlarmSnoozeState] intValue] == ALARMSNOOZESTATE_OFF)){

        NSString *stringForRepetAlarmDays = [dictionaryForAlarm objectForKey:kAlarmRepeat];

        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitWeekOfYear |  NSCalendarUnitHour | NSCalendarUnitMinute| NSCalendarUnitSecond | NSCalendarUnitWeekday) fromDate: [dictionaryForAlarm objectForKey:kAlarmSnoozeDate]];

        if([stringForRepetAlarmDays intValue] == REPEATALARM_ALL){

            // every day
            localNotification.repeatInterval = NSCalendarUnitDay;
            localNotification.fireDate=[calendar dateFromComponents:componentsForFireDate];
            [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

            [self repetativeLocalnotificationDictionary:dictionaryForAlarm andFireDate:[calendar dateFromComponents:componentsForFireDate] withRepeatInterval:NSCalendarUnitDay isForSnooze:NO];

        }else{

            NSArray *arrayForRepeatDays = [stringForRepetAlarmDays componentsSeparatedByString:@","]; // geting the days array @"1,2,3.." => [@"1",@"2",.]

            if(arrayForRepeatDays.count > 1){

                // custom days
                for (NSString *dayOfWeek in arrayForRepeatDays) {

                    // Creating multiple objects for each day to fire //$$$$$
                    UILocalNotification *localNotificationRepeat = [[UILocalNotification alloc]init];

                    [localNotificationRepeat setUserInfo:dictionaryForAlarm];
                    [localNotificationRepeat setAlertBody:[NSString stringWithFormat:kDefaultNotificationMessage]];
                    [localNotificationRepeat setSoundName:[NSString stringWithFormat:@"%@.%@",[dictionaryForAlarm objectForKey:kAlarmSoundName],[[GlobalClass getSoundDetails:[dictionaryForAlarm objectForKey:kAlarmSoundName]] objectForKey:kSoundExtention]]];
                    [localNotificationRepeat setTimeZone:[NSTimeZone localTimeZone]];

                    // setting weather to provide snooze option or not
                    if([[dictionaryForAlarm objectForKey:kAlarmSnooze] integerValue]){

                        [localNotificationRepeat setCategory:kNotificationCategoryIdentity];
                    }

                    [componentsForFireDate setWeekday:[dayOfWeek integerValue]];
                    localNotificationRepeat.repeatInterval = NSCalendarUnitWeekOfYear;;
                    localNotificationRepeat.fireDate=[calendar dateFromComponents:componentsForFireDate];
                    [[UIApplication sharedApplication] scheduleLocalNotification:localNotificationRepeat];

                    [self repetativeLocalnotificationDictionary:dictionaryForAlarm andFireDate:[calendar dateFromComponents:componentsForFireDate] withRepeatInterval:NSCalendarUnitWeekOfYear isForSnooze:NO];
                }

            }else{

                // single repeat
                [componentsForFireDate setWeekday: [arrayForRepeatDays[0] intValue]] ;
                localNotification.repeatInterval = NSCalendarUnitWeekOfYear;
                localNotification.fireDate=[calendar dateFromComponents:componentsForFireDate];
                [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

                [self repetativeLocalnotificationDictionary:dictionaryForAlarm andFireDate:[calendar dateFromComponents:componentsForFireDate] withRepeatInterval:NSCalendarUnitWeekOfYear isForSnooze:NO];
            }

        }

    }else{

        [localNotification setFireDate:[dictionaryForAlarm objectForKey:kAlarmSnoozeDate]];
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

        [self repetativeLocalnotificationDictionary:dictionaryForAlarm andFireDate:[dictionaryForAlarm objectForKey:kAlarmSnoozeDate] withRepeatInterval:NSCalendarUnitDay isForSnooze:NO];
    }

}


- (void)cancelAlarmNotification:(NSString *)stringForNotificationID {

    UIApplication *app = [UIApplication sharedApplication];
    NSArray *arrayForNotifications = [app scheduledLocalNotifications];

    for (int i=0; i<[arrayForNotifications count]; i++)
    {
        UILocalNotification* localNotification = [arrayForNotifications objectAtIndex:i];

        if ([[localNotification.userInfo valueForKey:kAlarmID] isEqualToString:stringForNotificationID])
        {
            //Cancelling local notification
            [app cancelLocalNotification:localNotification];
        }
    }
}

-(void)repetativeLocalnotificationDictionary:(NSDictionary *) notificationInfo andFireDate:(NSDate *)fireDate withRepeatInterval:(NSCalendarUnit) repeatInterval   isForSnooze:(BOOL) fromSnooze {

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

    [repeatNotification setFireDate:[NSDate dateWithTimeInterval:kDefaultImmediateNotificationTime sinceDate:fireDate]];
    [repeatNotification setUserInfo:notificationInfo];
    [repeatNotification setAlertBody:[NSString stringWithFormat:kDefaultNotificationMessage]];
    [repeatNotification setSoundName:[NSString stringWithFormat:@"%@.%@",[notificationInfo objectForKey:kAlarmSoundName],[[GlobalClass getSoundDetails:[notificationInfo objectForKey:kAlarmSoundName]] objectForKey:kSoundExtention]]];
    [repeatNotification setTimeZone:[NSTimeZone localTimeZone]];
    [repeatNotification setRepeatInterval:repeatInterval];


    // setting weather to provide snooze option or not
    if([[notificationInfo objectForKey:kAlarmSnooze] integerValue]){

        [repeatNotification setCategory:kNotificationCategoryIdentity];
    }


    [[UIApplication sharedApplication] scheduleLocalNotification:repeatNotification];
}
-(void)setAlarmNotification:(NSDictionary*)dictionaryForAlarm{
UILocalNotification*localNotification=[[UILocalNotification alloc]init];
[localNotification setUserInfo:dictionaryForAlarm];
[localNotification setAlertBody:[NSString stringWithFormat:kDefaultNotificationMessage]];
[localNotification setSoundName:[NSString stringWithFormat:@“%@.%@,[DictionaryForarm objectForKey:kAlarmSoundName],[GlobalClass getSoundDetails:[DictionaryForarm objectForKey:kAlarmSoundName]]objectForKey:KSoundExtension]];
[localNotification setTimeZone:[NSTimeZone localTimeZone]];
//设置天气是否提供打盹选项
if([[dictionaryForAlarm objectForKey:kAlarmSnooze]integerValue]){
[localNotification setCategory:kNotificationCategoryIdentity];
}
//将通知设置为repeat/并检查其是否未触发休眠通知
如果(([[dictionaryForAlarm objectForKey:kAlarmRepeatState]intValue]==AlarmRepeatState\u打开)和([dictionaryForAlarm objectForKey:kAlarmSnoozeState]intValue]==ALARMSNOOZESTATE\u关闭)){
NSString*stringForRepetAlarmDays=[DictionaryForAlarmObjectForkey:kAlarmRepeat];
NSCalendar*日历=[NSCalendar currentCalendar];
NSDateComponents*components ForFireDate=[日历组件:(NSCalendarUnitYear | NSCalendarUnitWeekOfYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday)fromDate:[字典forAlarmObjectForkey:KalarmsnozeDate];
如果([stringForRepetAlarmDays intValue]==重复报警\u全部){
//每天
localNotification.repeatInterval=NSCalendarUnitDay;
localNotification.fireDate=[CalendarDateFromComponents:componentsForFireDate];
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
[self-RepeatativeVelocialNotificationDictionary:dictionaryForAlarm and FireDate:[Components:Components ForFireDate]的日历日期,重复间隔:NSCalendarUnitDay为Noze:NO];
}否则{
NSArray*arrayForRepeatDays=[StringForRepeatArmDays组件由字符串分隔:@“,”];//获取天数数组@“1,2,3..=>[@“1”,“2”,“2”。]
如果(arrayForRepeatDays.count>1){
//海关日
for(NSString*dayOfWeek在arrayForRepeatDays中){
//每天创建多个要激发的对象//$$$$$
UILocalNotification*localNotificationRepeat=[[UILocalNotification alloc]init];
[localNotificationRepeat setUserInfo:dictionaryForAlarm];
[localNotificationRepeat setAlertBody:[NSString stringWithFormat:kDefaultNotificationMessage]];
[localNotificationRepeat setSoundName:[NSString stringWithFormat:@“%@.%@,[dictionaryForAlarm objectForKey:kAlarmSoundName],[GlobalClass getSoundDetails:[dictionaryForAlarm objectForKey:kAlarmSoundName]]objectForKey:KSoundExtension]];
[localNotificationRepeat setTimeZone:[NSTimeZone localTimeZone]];
//设置天气是否提供打盹选项
if([[dictionaryForAlarm objectForKey:kAlarmSnooze]integerValue]){
[localNotificationRepeat setCategory:kNotificationCategoryIdentity];
}
[componentsForFireDate setWeekday:[dayOfWeek integerValue]];
localNotificationRepeat.repeatInterval=NSCalendarUnitWeekOfYear;;
localNotificationRepeat.fireDate=[CalendarDateFromComponents:componentsForFireDate];
[[UIApplication sharedApplication]scheduleLocalNotification:localNotificationRepeat];
[self-RepeatativeVelocialNotificationDictionary:dictionaryForAlarm and FireDate:[Components:Components ForFireDate]的日历日期,重复间隔为:NSCalendarUnitWeekOfYear for Noze:NO];
}
}否则{
//单次重复
[componentsForFireDate setWeekday:[arrayForRepeatDays[0]intValue]];
localNotification.repeatInterval=NSCalendarUnitWeekOfYear;
localNotification.fireDate=[CalendarDateFromComponents:componentsForFireDate];
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
[self-RepeatativeVelocialNotificationDictionary:dictionaryForAlarm and FireDate:[Components:Components ForFireDate]的日历日期,重复间隔为:NSCalendarUnitWeekOfYear for Noze:NO];
}
}
}否则{
[localNotification setFireDate:[dictionaryForAlarm objectForKey:kAlarmSnoozeDate];
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
[self-RepeatativeVelocialNotificationDictionary:dictionaryForAlarm and FireDate:[dictionaryForAlarm objectForKey:kAlarmSnoozeDate]带重复间隔:NSCalendarUnitDay为Noze:NO];
}
}
-(无效)cancelAlarmNotification:(NSString*)stringForNotificationID{
UIApplication*app=[UIApplication sharedApplication];
NSArray*arrayForNotifications=[app scheduledLocalNotifications];

对于(int i=0;i这是通知每天发出的代码

-(void) scheduleLocalNotificationDaily:(NSDate *)fireDate
{

       NSDictionary * infoDict = @{
                     @"alarmUiqueId" : uID,

                     };

    NSDateComponents *comp = [[NSCalendar currentCalendar] components:NSCalendarUnitSecond
                                                             fromDate:fireDate];
    fireDate = [fireDate dateByAddingTimeInterval:-comp.second];
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = fireDate;
    localNotif.timeZone = [NSTimeZone localTimeZone];    
    localNotif.alertBody = @"Time to wake Up";
    localNotif.alertAction = @"Show me";

    localNotif.soundName = @"3D bird tone.mp3";


        localNotif.userInfo = infoDict;

    localNotif.repeatInterval = NSCalendarUnitDay;
    NSLog(@" date %lu",kCFCalendarUnitDay);
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];


}
以及取消开关或其他特定通知

for(UILocalNotification *notification in notificationArray)
            {

                NSLog(@"%@",[notification.userInfo valueForKey:@"alarmUiqueId"]);
                NSLog(@"%@",alrm.uniqueId);
                if ([[notification.userInfo valueForKey:@"alarmUiqueId" ] isEqualToNumber: alrm.uniqueId])
                {
                    [[UIApplication sharedApplication] cancelLocalNotification:notification ] ;
                }
            }

你应该传递一个唯一的id来取消通知。是的,我已经这样做了,但这也会取消以后的重复