当应用程序启动时,UILocalNotification会被重复调用

时间:2015-09-07 12:10:23

标签: ios localnotification

enter image description here我不知道这里的问题是什么,但每次我运行我的应用时,本地通知会被重复显示为fireDate为NULL。我正在尝试从过去5小时开始处理这个问题now.I需要帮助!!!

    "<UIConcreteLocalNotification: 0x7f872ead7630>{fire date = (null), time zone = (null), repeat interval = NSCalendarUnitDay, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = Monday, 7 September 2015 5:32:44 pm India Standard Time, user info = (null)}",

我已经对UILocalNotification进行了足够的研究以开始使用它,但我仍然面临这个问题。

    -(void)setDate:(NSDate*)myfireDate andTime1InString:       (NSString*)time1Str andTime2InString:(NSString*)time2Str andTime3InString:(NSString*)time3Str{

    //concatenate myFireDate with all three times one by one
NSString *myFireDateInString = [dateFormatter stringFromDate:myfireDate];
myFireDateInString = [myFireDateInString stringByAppendingString:@" "];
NSString *dateWithTime1InString = [myFireDateInString stringByAppendingString:time1Str];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateWithTime1 = [dateFormatter dateFromString:dateWithTime1InString];

NSString *mySecondFireDateInString;
NSString *dateWithTime2InString;
NSDate *dateWithTime2;
if ([time2Str length] !=0){
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    mySecondFireDateInString = [dateFormatter stringFromDate:myfireDate];
    mySecondFireDateInString = [mySecondFireDateInString stringByAppendingString:@" "];
    dateWithTime2InString = [mySecondFireDateInString stringByAppendingString:time2Str];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    dateWithTime2 = [dateFormatter dateFromString:dateWithTime2InString];
}

NSString *myThirdFireDateInString;
NSString *dateWithTime3InString;
NSDate *dateWithTime3;
if ([time3Str length]!=0){
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    myThirdFireDateInString = [dateFormatter stringFromDate:myfireDate];
    myThirdFireDateInString = [myThirdFireDateInString stringByAppendingString:@" "];
    dateWithTime3InString = [myThirdFireDateInString stringByAppendingString:time3Str];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    dateWithTime3 = [dateFormatter dateFromString:dateWithTime3InString];
}

NSLog(@"%@",dateWithTime3);
NSLog(@"%@",dateWithTime2);
    //block starts here
    void(^notificationBlock)(void) = ^{

appDelegate.localNotification1 = [UILocalNotification new];
appDelegate.localNotification1.fireDate = dateWithTime1;
appDelegate.localNotification1.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;

if(dateWithTime2 != nil){//Make a new UILocalNotification object

    appDelegate.localNotification2 = [UILocalNotification new];
    appDelegate.localNotification2.fireDate = dateWithTime2;
    appDelegate.localNotification2.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
}

if(dateWithTime3 !=nil){//MAke a new UILocalNotification object
   appDelegate.localNotification3 = [UILocalNotification new];
   appDelegate.localNotification3.fireDate = dateWithTime3;
   appDelegate.localNotification3.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
}



if([_repeatDaysTextField.text isEqualToString:@"Everyday"]){

    // appDelegate.localNotification1.alertBody = @"Time to take your   medicine";
    appDelegate.localNotification1.repeatInterval = kCFCalendarUnitDay;
    [[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification1];

    if(dateWithTime2 != nil){
        appDelegate.localNotification2.repeatInterval = kCFCalendarUnitDay;
       [[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification2];
        appDelegate.localNotification2.alertBody = @"Not2";
        NSLog(@"%@",appDelegate.localNotification2);
    }
    NSLog(@"%@",[NSString stringWithFormat:@"%@",dateWithTime3 ]);
    if(dateWithTime3 != nil){

        [[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification3];
    }
}

else if([_repeatDaysTextField.text isEqualToString:@"Alternately"]){

}

else if([_repeatDaysTextField.text isEqualToString:@"Weekly"]){
    appDelegate.localNotification1.repeatInterval = kCFCalendarUnitWeekday;
    [[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification1];
    if(dateWithTime2!=nil){
        [[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification2];
    }
    if(dateWithTime3!=nil){
        [[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification3];
    }
}

else if([_repeatDaysTextField.text isEqualToString:@"Bi-Weekly"]){
}

else if([_repeatDaysTextField.text isEqualToString:@"Monthly"]){
     appDelegate.localNotification1.repeatInterval = kCFCalendarUnitMonth;
     [[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification1];
    if(dateWithTime2!=nil){
        [[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification2];
    }
    if(dateWithTime3!=nil){
        [[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification3];
    }
}

else if([_repeatDaysTextField.text isEqualToString:@"Yearly"]){
    appDelegate.localNotification1.repeatInterval = kCFCalendarUnitYear;
    [[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification1];
    if(dateWithTime2!=nil){
        [[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification2];
    }
    if(dateWithTime3!=nil){
        [[UIApplication sharedApplication]scheduleLocalNotification:appDelegate.localNotification3];
    }
    }
    };
    //block ends here

    //method to set notification
   [self setNotification:notificationBlock];
    }




    -(void)setNotification:(void(^)(void))setNotificationBlock{

    setNotificationBlock();
    }

2 个答案:

答案 0 :(得分:1)

我得到了解决方案。在方法中,didReceiveLocalNotification :,我将通知对象添加到'scheduledNotifications'数组。所以每次安排通知时,同一个对象都被添加到'scheduledNotifications'数组中一次又一次被解雇。

    [UIApplication sharedApplication]scheduledNotifications = notification;

注意:在didReceiveLocalNotification中忽略此语句:

答案 1 :(得分:0)

这是因为你正在设置

appDelegate.localNotification.repeatInterval = someUnit;

尝试通过检查repeatCount是否为0来尝试将repeatInterval值设置为1。