2015-09-07 47 views
1

enter image description here我不知道这里有什么问题,但是每次运行我的应用程序时,本地通知都会调用重复显示的fireDate作为NULL.I正试图从过去5小时了。我需要帮助!UILocalNotification在应用程序启动时被重复调用

"<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(); 
    } 
+0

,你必须设置通知 – baydi

+0

错误可能发生在这里的代码粘贴重复计数为UILocalNotificationInfiniteRepeatCount检查一次你的UILocalNotification –

+0

的计数这里是我的代码.. ... – Reckoner

回答

0

这是发生怎么一回事,因为您要设置

appDelegate.localNotification.repeatInterval = someUnit;

尝试通过检查repeatCount是否为0或不按repeatInterval值设置为1。

+0

您只能设置repeatInterval属性的预定义枚举。 – Reckoner

+0

是的,我读了这个。设置0作为一个值可以防止无限重复计数,但它不起作用。 – Reckoner

1

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

[UIApplication sharedApplication]scheduledNotifications = notification; 

注:忽略didReceiveLocalNotification这一说法:

相关问题