Невозможно запланировать ежедневное местное PushNotification
. Я хочу показать только одну ежедневную локальную PushNotification в 9:00 с подсчитанными задачами на сегодняшний день.Как планировать ежедневное локальное push-уведомление в iOS (ObjC)?
Мой код выполняется только один раз в didFinishLaunchingWithOptions
как:
- (void)scheduleLocalNotification
{
self.localNotification = [UILocalNotification new];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];
components.hour = 9;
components.minute = 0;
components.second = 0;
NSDate *fireTime = [calendar dateFromComponents:components];
_localNotification.fireDate = fireTime;
_localNotification.alertBody = [NSString stringWithFormat:@"Hi, <username>. You have %ld tasks for today", (long)_todayTasks];
_localNotification.repeatInterval = NSCalendarUnitDay;
_localNotification.soundName = @"alarm.wav";
_localNotification.timeZone = [NSTimeZone localTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:_localNotification];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm"];
NotificationManagerLog(@"schedule local notification at %@", [dateFormatter stringFromDate:fireTime]);
}
Похоже, я что-то пропустил, потому что это действительно срабатывает в 9:00, но с неправильными данными в _todayTasks
, он также может быть случайным образом срабатывает с другими _todayTasks
и повторяется несколько раз в произвольное время.
Это, кажется, работает, спасибо. –