我想在每个星期六这样显示本地通知,
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;
}
但我的本地通知会显示在每个小工具中,那么我如何在每周的每个星期六显示通知。 感谢。
答案 0 :(得分:0)
尝试在fireData
中声明localNotification
属性并安排通知
localNotification.fireDate = componentsForFireDate.date;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
希望这个帮助