ios - UILocalNotification 在应用程序启动时被重复调用

标签 ios localnotification

enter image description here我不知道这里的问题是什么,但每次我运行我的应用程序时,都会重复调用本地通知,将 fireDate 显示为 NULL。我正在努力解决过去 5 小时以来的这个问题。我需要帮助!!!

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

最佳答案

我得到了解决方案。在方法中,didReceiveLocalNotification:,我将通知对象添加到“scheduledNotifications”数组中。因此每次安排通知时,都会将相同的对象添加到“scheduledNotifications”数组中一次又一次地被解雇。

    [UIApplication sharedApplication]scheduledNotifications = notification;

注意:忽略didReceiveLocalNotification中的这条语句:

关于ios - UILocalNotification 在应用程序启动时被重复调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32438422/

相关文章:

ios - 从选择表格单元格设置本地通知

ios - 如何在 Swift 2.0 中将 [String : String?] 转换为 [NSObject : AnyObject]! for Firebase Api?

ios - 当我的 SSL 证书过期后,ASIFormDataRequest 是否仍然有效?

ios - 如果在多个单独的线程上调用,则dispatch_once()

iphone - 如何将自定义 tableView 搜索工具与 UISearchDisplayController 结合起来?

ios - Polidea/ios-class-guard - 如何排除外部类

ios - 取消具有重复间隔的 UILocalNotification

android - Cordova 本地通知

ios - 重复本地通知会删除之前待处理的本地通知

ios - Swift 通知 - 触发日期后的重复间隔