사용자가 앱을 처음 열 때 날짜를 저장하고 30 일 동안 특정 시간에 매일 로컬 알림을 보내려는 앱이 있습니다. 매일 매일 달라야합니다. 각 메시지는 해당 날짜 이벤트의 알림입니다.설정된 일 수에 대한 다른 UIlocalnotification을 실행합니다.
은 AppDelegate에 나는 그때 현재 날짜에 하루 추가하고 내 schedulenotificatons 메소드를 호출 루프를 가지고
loadNotif 방법에- (void)applicationDidEnterBackground:(UIApplication *)application {
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [NSDate date];
NSLog(@"date : %@",date);
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone]; // <- Local time zone
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date];
NSLog(@"date : %@",destinationDate);
[self loadNotif:destinationDate];
}
이 메소드를 호출 현재 시간에 날짜를 설정
-(void)loadNotif:(NSDate *)notifDate {
NSLog(@" date %@", notifDate);
NSString *alertMSG = @"";
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
for (int i = 0; i <= 3; i++) {
dayComponent.second = i;
if (i == 0) {
alertMSG = @"Checkout Today's Video! Sustainable Posture!";
} else if (i == 1) {
alertMSG = @"Checkout Today's Video! Forward Head!";
} else if (i == 2) {
alertMSG = @"Checkout Today's Video! Neck Position!";
} else if (i == 3) {
alertMSG = @"Checkout Today's Video! Shoulder Position!";
}
notifDate = [notifDate dateByAddingTimeInterval:5];
[self scheduleNotificationForDate:notifDate AlertBody:alertMSG ActionButtonTitle:@"30 Day Posture Challenge" NotificationID:alertMSG];
NSLog(@"nextDate: %@ ... %@", notifDate, alertMSG);
}
}
(테스트 목적) 나는 그들이 작동하는지 확인하기 위해 날짜에 5 초만 추가합니다.
-(void) scheduleNotificationForDate:(NSDate *)date AlertBody:(NSString *)alertBody ActionButtonTitle:(NSString *)actionButtonTitle NotificationID:(NSString *)notificationID{
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
NSLog(@"firedate %@", date);
localNotification.fireDate = date;
localNotification.timeZone = [NSTimeZone localTimeZone];
localNotification.alertBody = alertBody;
localNotification.alertAction = actionButtonTitle;
localNotification.soundName=UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber= + 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:@"notifID"];
localNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
내가 겪고있는 문제는 모든 통지가 동시에 발생하는 것입니다. 왜 이런 일이 일어나는가?
로그 메시지의 내용은 무엇입니까? – matt
로그 메시지는 firedate가 올바르게 작동 함을 보여줍니다. 5 초마다 각 루프 추가 2016-10-20 20 : 10 : 41.328 firedate 2016-10-20 20:10:46 +0000 firedate 2016-10-20 20:10:51 +0000 –
내가 생각할 수있는 유일한 것 어쩌면 5 초가 너무 세밀합니다. 시스템은 사용자를 대신하여 이러한 것을 제공하므로 함께 묶는 방법에 대한 판단을 내립니다. 그래서 그것들을 목적에 맞게 조합 할 수 있습니다. 그래서, 그것이 고통이 될지라도, 잠시 떨어져 놓고 어떻게 가는지보십시오. – matt