在iOS中显示特定日期和特定时间的本地通知

时间:2016-07-18 12:38:50

标签: ios notifications uilocalnotification

我想在每个星期六这样显示本地通知,

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat=@"EEEE";
        NSString *dayString = [[dateFormatter stringFromDate:[NSDate date]] capitalizedString];
        NSLog(@"day: %@", dayString);

        if([dayString isEqualToString:@"Saturday"])
        {
            NSLog(@"Success");
            [self PushNotification];
        }
-(void)PushNotification
{
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.alertTitle = @"Test";
        localNotification.alertBody =@"test of notification";


        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        NSDateComponents *componentsForFireDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitWeekOfMonth |  NSCalendarUnitHour | NSCalendarUnitMinute| NSCalendarUnitSecond | NSCalendarUnitWeekday) fromDate: [NSDate date]];
        [componentsForFireDate setWeekday: 7]; //for fixing Saturday
        [componentsForFireDate setHour: 17]; //for fixing 5PM hour
        [componentsForFireDate setMinute:0];
        [componentsForFireDate setSecond:0];

        localNotification.repeatInterval = NSCalendarUnitWeekOfMonth; 
    }

但我的本地通知会显示在每个小工具中,那么我如何在每周的每个星期六显示通知。 感谢。

1 个答案:

答案 0 :(得分:0)

尝试在fireData中声明localNotification属性并安排通知

localNotification.fireDate = componentsForFireDate.date;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

希望这个帮助