为设定的天数激发不同的UIlocalnotification

时间:2016-10-21 02:15:53

标签: ios objective-c uilocalnotification

我有一个应用程序,当用户第一次打开应用程序时,我保存了日期,我希望每天在特定时间发送本地通知30天。每天都需要与众不同。每条消息都提醒着那天的事件。

在Appdelegate中,我将日期设置为当前时间,然后调用方法

- (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];

}
在loadNotif方法中的

是我有一个for循环,它将一天添加到当前日期并调用我的schedulenotificatons方法

-(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];

}

我遇到的问题是所有通知同时发生。任何线索为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

我打算走出困境,并建议所有那些带有GMT和时区的jiggery-pokery都会让你感到烦恼。我不知道它应该做什么,但它让我的眼睛越过了。从现在起五秒钟安排本地通知是一行的:

ln.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];

我建议你坚持这样的事情。只需将秒数传递给通知计划程序即可。