当一天过去时,UILocalNotification applicationIconBadgeNumber不起作用

时间:2016-02-22 07:47:55

标签: ios objective-c

如果没有更改日期,以下代码一切正常。但是当改变时,将徽章2改为放置1。

你知道为什么会这样吗?

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = startDate;
        localNotification.soundName = UILocalNotificationDefaultSoundName;
        localNotification.alertBody =  [NSString stringWithFormat:@"%@ '%@'.", NSLocalizedString (@"Tiene tareas pendientes para realizar en su acuario", ""), descripcionAcuario];
        localNotification.alertAction = descripcionTextField.text;
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

        NSDictionary *inventory = @{
                                    @"AcuarioID" : [NSNumber numberWithInt: acuarioSeleccionadoID],
                                    @"TareaID" : [NSNumber numberWithInt: tareaSeleccionada],
                                    };

        localNotification.userInfo= inventory;
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

1 个答案:

答案 0 :(得分:0)

dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSString *alarmTimeStr = [[NSString alloc]init];
alarmTimeStr = [NSString stringWithFormat:@"%@",alarmTimeTextField.text];

  NSDate  *pickerdate = dateTimePicker.date;
[[NSCalendar currentCalendar] rangeOfUnit:NSMinuteCalendarUnit
                                startDate:&pickerdate
                                 interval:NULL
                                  forDate:pickerdate];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
pickerDate1 = pickerdate;
finaldate =pickerDate1;

// ** Local notification Assignment **
localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
    return;

localNotification.fireDate = pickerDate1;
localNotification.alertBody = alarmNameTextField.text;
localNotification.alertAction = @"OK";

localNotification.soundName = [NSString stringWithFormat:@"%@.mp3",alarmTonesTextField.text];

localNotification.timeZone = [NSTimeZone systemTimeZone];
[localNotification setHasAction:YES];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

 [self setalarm];
-(void)setalarm
{
        if ([sunValue isEqualToString:@"1"])
        {
        finaldate =pickerDate1;
        [self getDateOfSpecificDay:1];
        [self setweek];
    }
    if ([monValue isEqualToString:@"1"])
    {
        finaldate =pickerDate1;
        [self getDateOfSpecificDay:2];
        [self setweek];
    }
    if ([tueValue isEqualToString:@"1"])
    {
        finaldate =pickerDate1;
        [self getDateOfSpecificDay:3];
        [self setweek];

    }
    if ([wedValue isEqualToString:@"1"])
    {
        finaldate =pickerDate1;
        [self getDateOfSpecificDay:4];
        [self setweek];
    }
    if ([thuValue isEqualToString:@"1"])
    {
        finaldate =pickerDate1;
        [self getDateOfSpecificDay:5];
        [self setweek];
    }
    if ([friValue isEqualToString:@"1"])
    {
        finaldate =pickerDate1;
        [self getDateOfSpecificDay:6];
        [self setweek];
    }
    if ([satValue isEqualToString:@"1"])
    {
        finaldate =pickerDate1;
        [self getDateOfSpecificDay:7];
        [self setweek];
    }
    alarmArray = [[DBManager sharedDatabase] getAlarmsDetail];
    if ([alarmArray count] != 0)
    {
        [self showAllAlarmsInAlarmView];
    }
if ([alarmArray count]==0)
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

}
-(void)setweek
{
@try
{
NSDateFormatter *formatterdate=[[NSDateFormatter alloc]init];
[formatterdate setDateFormat:@"YYYY-MM-dd"];

NSDateFormatter *formattertime =[[NSDateFormatter alloc]init];
[formattertime setDateFormat:@"HH:mm:ss"];

NSString * stringdate = [NSString stringWithFormat:@"%@",resultDate];
NSString *stringdate123 =[stringdate substringToIndex:10];

NSString *stringtime =[dateFormatter stringFromDate:pickerDate1];
NSString * stringtime123 =[stringtime substringFromIndex:11];

NSString * stringdate1234=[stringdate123 stringByAppendingString:@" "];
NSString *  stringdate12345=[stringdate1234 stringByAppendingString:stringtime123];

finaldate =[dateFormatter dateFromString:stringdate12345];

localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil)
        return;
localNotification.fireDate = finaldate;
localNotification.alertBody = alarmNameTextField.text;
localNotification.repeatInterval =NSWeekCalendarUnit;
localNotification.alertAction = @"OK";

localNotification.soundName = [NSString stringWithFormat:@"%@.mp3",alarmTonesTextField.text];//@"Jai Hanuman.mp3";

localNotification.timeZone = [NSTimeZone systemTimeZone];
[localNotification setHasAction:YES];

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

}

@catch (NSException *exception)
{
    [[saveClass shared_class] writeStringToFile:@"alarmViewController" andfunctionname:@"saveAlarmAndSetNotification" andexception:(NSString*)exception];
}
@finally
{}
}

-(NSDate *) getDateOfSpecificDay:(NSInteger ) day /// here day will be 1 or 2.. or 7
{
NSInteger desiredWeekday = day;
NSRange weekDateRange = [[NSCalendar currentCalendar] maximumRangeOfUnit:NSWeekdayCalendarUnit];
NSInteger daysInWeek = weekDateRange.length - weekDateRange.location + 1;

NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:finaldate];

NSLog(@"%@",[NSDate date]);
NSInteger currentWeekday = dateComponents.weekday;
if (desiredWeekday == currentWeekday)
{
    differenceDays = 7;
    daysComponents = [[NSDateComponents alloc] init];
}
else
{
    differenceDays = (desiredWeekday - currentWeekday + daysInWeek) % daysInWeek;
    daysComponents = [[NSDateComponents alloc] init];
}

daysComponents.day = differenceDays;
resultDate = [[NSCalendar currentCalendar] dateByAddingComponents:daysComponents toDate:finaldate options:0];
return resultDate;
}